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>

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to