How to initialize a class variable once

Brian Allen Vanderburg II BrianVanderburg2 at
Tue Dec 9 12:31:12 CET 2008

mccredie at wrote:
> Unless you are calling reload() on the module, it will only ever get
> _loaded_ once. Each additional import will just yield the existing
> module. Perhaps if you post an example of the behavior that leads you
> to believe that the class variables are getting reinitialized I can
> provide more useful help.
> Matt
> --

There is one situation where a module can be imported/executed twice, if 
it is the __main__ module.  Obviously the example below would be 
considered bad Python practice but it just shows how it can be done:

class Blah(object):
    def action(self):
        print "action"

print "import"

if __name__ == "__main__":
    import app

def run():
    import main
    blah = main.Blah()



The reason is the first time gets loaded, it is known as 
'__main__' but when app imports main, it is not in sys.modules so it 
loads '' again but this time as 'main'

Brian Vanderburg II

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list