How do you make issubclass work
Nathan Bullock
nathan_kent_bullock at yahoo.ca
Sat Sep 11 14:56:43 EDT 2004
I think my first post was a little unclear. I will try again.
I have two files, the first is this:
File b.py:
-------------------
from a import cla_a
class cla_b(cla_a): pass
-------------------
File a.py
-------------------
class cla_a(object): pass
class cla_c(cla_a): pass
if __name__ == "__main__":
import b as mod
print issubclass(mod.cla_b, cla_a)
print issubclass(mod.cla_b, mod.cla_a)
print issubclass(cla_c, cla_a)
print mod.cla_a is cla_a
-------------------
Results of 'python a.py'
False
True
True
False
-----------------------------
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? And do to this problem how
in the world do I determine if a cla_b is a subclass of cla_a?
Obviously they are, but issubclass doesn't realize it because the
cla_a that I am testing with is not the same instance of cla_a as
cla_b was derived from.
>
> [As modules are cached, subsequent imports of the same module yield the same
> module instance. Therefore the same classes (cla_a is cla_a == True) are
> seen by all parts of a program and it (normally) doesn't matter in what
> module a class is defined.]
>
> Peter
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.
Nathan
More information about the Python-list
mailing list