Messing up with classes and their namespace
tjreedy at udel.edu
Fri Jun 5 23:07:39 CEST 2009
Jean-Michel Pichavant wrote:
> Thanks for the explanation. I'll have to give it a second thought, I'm
> still missing something but I'll figure it out.
Perhaps it is this:
1. When you run foo.py as a script, the interpreter creates module
'__main__' by executing the code in foo.py.
2. When that code does 'import lib', the interpreter looks for an
existing module named 'lib', does not find it, and creates module 'lib'
by executing the code in lib.py.
3. When that code does 'import foo', the interpreter looks for an
existing module named 'foo', does not find it, and creates module 'foo'
by executing (again) the code in foo.py.
Module 'foo' is slightly different from module '__main__', created from
the same code, because of the section conditioned by 'if __name__ ==
'__main__', that being the purpose of that incantation. But each of the
two modules have their own class Foo. You sort of guessed this ...
> I guess there is 2 different objects for the same class Foo.
They are the same in content, but not in identify, until you change one
> How I do I make both Foo objects the same object ?
As Scott hinted, by not making two of them, and you do that by not
making two modules from the same file.
Terry Jan Reedy
More information about the Python-list