[Python-Dev] avoiding accidental shadowing of top-level libraries by the main module
Glyph Lefkowitz
glyph at twistedmatrix.com
Tue Jul 13 23:36:12 CEST 2010
On Jul 13, 2010, at 5:02 PM, Nick Coghlan wrote:
> My concerns aren't about a module reimporting itself directly, they're
> about the case where a utility module is invoked as __main__ but is
> also imported normally somewhere else in a program (e.g. pdb is
> invoked as a top-level debugger, but is also imported directly for
> some reason). Currently that works as a non-circular import and will
> only cause hassles if there is top-level state in the affected module
> that absolutely must be a singleton within a given application. Either
> change (disallowing it completely as you suggest, or making it a
> circular import, as I suggest) runs the risk of breaking code that
> currently appears to work correctly.
>
> Fred's point about the practice of changing __name__ in the main
> module corrupting generated pickles is one I hadn't thought of before
> though.
It's not just pickle; anything that requires __name__ (or __module__) to be accurate for introspection or debugging is also problematic.
I have long considered it a 'best practice' (ugh, I hate that phrase, but I can't think of what else to call it) to _always_ do this type of shadowing, and avoid defining _any_ names in the __name__ == '__main__' case, so that there's no ambiguity:
<http://glyf.livejournal.com/60326.html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20100713/649a8653/attachment-0001.html>
More information about the Python-Dev
mailing list