How do you make issubclass work
Andrew Dalke
adalke at mindspring.com
Sat Sep 11 15:59:38 EDT 2004
Nathan Bullock wrote:
> This is using python 2.3.3. Is this a bug? Why do we have two
> different cla_a's which are not the same?
Your file 'a.py' is two things. It's the code being used
as the "__main__" module *and* it's used as the "a" module.
Add this to your a.py:__main__ to see the difference.
import sys
main_module = sys.modules["__main__"]
print "main is from", main_module.__file__
a_module = sys.modules["a"]
print "a is from", a_module.__file__
print "Are the modules the same?", a_module == main_module
print "Are the classes the same?", a_module.cla_a ==
main_module.cla_a
You'll see that the file a.py is used twice, and the
class cla_a defined once as __main__.cla_a and the other
as a.cla_a .
To make what you want work, well, a good rule is to avoid
circular imports. Another is that your main code should
not be imported. But if you want so see your code to work
as you expect it to work, you need to change b.py so that
the "from a import cla_a" is instead
"from __main__ import cla_a"
> Now I take it from this answer from Peter, that if I did the test in a
> third file so that I was using an imported instance of cla_a that
> issubclass would then work properly.
It should.
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list