"Mac" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] >I see, interesting. OK, I understand that recursive importing can be > problematic (having to "hide" the test2 import should have been a tip > off; it's just that in my original app this relationship is not as > clear), but what is the lesson I should take away from this?
I suspect that the import hiding was needed to avoid infinite recursion but is not essential in itself to getting duplicate class Foo objects. > I mean, I > was under the impression that "once a Foo, always a Foo", while from > the above I'm starting to see that a single class definition can give > rise to a multiple number of classes, Unless you intend this, it is probably a programming error on your part. > and that the classes are > parametrized by the module they come from (I guess that makes sense... > else class names would have to be unique throughout all the source for > a single program) There is nothing special about classes here. >... I guess the problem is I'm thinking of "classes" > as these abstract concepts, sort of like Platonian "forms", whereas I > should be thinking of classes as "class objects", Definitely. In Python, 'everything' is an object. Understanding this is a key to understanding Python programming. > Someone help me wrap my head around this, please. :) Here is the source of your particular problem. Running 'python somefile.py' is more or less equivalent to a hidden single-line program: 'import somefile.py as __main__'. The code in somefile is used to populate the main module, named '__main__'. If the code in somefile.py (or .pyc) leads, directly or indirectly, to execution of 'import somefile', the import function looks for an existing module named (bound to, in sys.modules, for CPython) 'somefile'. Not finding one, it create a new module, names it 'somefile', and populates it from somefile.py. Now there are duplicate modules and probably a program bug. This is not the only way to get two modules from one file. One can give import different access paths to a file such that it will not recognize that it has already imported the file. But this too is almost certainly an error. One way people avoid importing the main module file after startup is to limit it to top-level code with no class or function definitions that might need to be imported elsewhere. But when a module of definitions, intended for import, has an "if __name__ == '__main__': test()" section for testing purposes, then more care may be needed. Terry J. Reedy -- http://mail.python.org/mailman/listinfo/python-list