Messing up with classes and their namespace

Jean-Michel Pichavant jeanmichel at sequans.com
Fri Jun 5 07:23:23 EDT 2009


Hello world,

I had recently a very nasty bug in my python application. The context is 
quite complex, but in the end the problem can be resume as follow:

2 files in the same directory :

lib.py:
 >import foo
 >foo.Foo.BOOM='lib'

foo.py:
 >class Foo:
 >    BOOM = 'Foooo'
 >
 >if __name__=='__main__':
 >    import lib # I'm expecting BOOM to be set to 'lib'
 >    print Foo.BOOM

I was expecting 'lib' as output, but I got 'Fooo'. I don't really 
understand what python mechanism I'm messing with but I have the feeling 
I've misunderstood a very basic concept about class, namespace or 
whatever import notion.

This is how I made it work:
 >if __name__=='__main__':
 >    from lib import Foo # make sure Foo comes from lib
 >    print Foo.BOOM


I guess there is 2 different objects for the same class Foo. How I do I 
make both Foo objects the same object ?

Jean-Michel



More information about the Python-list mailing list