How to initialize a class variable once
george.sakkis at gmail.com
Tue Dec 9 17:31:41 CET 2008
On Dec 9, 10:36 am, Joe Strout <j... at strout.net> wrote:
> On Dec 9, 2008, at 4:31 AM, Brian Allen Vanderburg II wrote:
> > There is one situation where a module can be imported/executed
> > twice, if it is the __main__ module.
> That's an excellent point -- this is something I've run into, and it
> always feels a bit awkward to code around it. What's the standard
> idiom for avoiding this issue in a complex app? Have a "main" file
> that is not imported by anything else, and which does little but
> launch stuff from some other module?
Yes, I believe that's the common practice. Still, whenever I end up
putting stuff in a "main" file and run into the double import problem
(e.g. when pickling), I do an explicit "from main import *" after all
the definitions, i.e.:
__all__ = ['foo', 'Bar']
from myscript import *
if __name__ == '__main__':
assert foo.__module__ == Bar.__module__ == 'myscript'
More information about the Python-list