when does issubclass fail?

Walter Dörwald walter at livinglogic.de
Tue Apr 20 09:03:07 EDT 2004


Chris Green wrote:

> Heyas folks,
> 
> When does issubclass fail?  That's a loaded question so here's my test
> case (also available at http://cmg.dok.org/code/classimports.tar.gz):
> 
> directory structure:
> 
> ./test.py
> ./d/__init__.py
> ./d/b.py
> ./c/__init__.py
> ./c/a.py
> 
> #--------[a.py]---------
> class A(object):
>     pass
> #------[end a.py]-------
> 
> #--------[b.py]---------
> import sys
> sys.path.append("/home/cmg/src/python/classtest/a")
> class B(a.A):
>     pass
> #------[end b.py]-------
> 
> #--------[test.py]------
> import c.a
> import d.b
> print issubclass(d.b.B,c.a.A)
> #--------[end test.py]--
> 
> issubclass(d.b.B,c.a.A)  => False
> 
> I'm trying to figure out why this is failing. My guess is that there
> are two distinct imports of a.A in memory though I can't come up with
> a simple test case that makes this happen without manipulating
> sys.path.

The simplest demo for this problem is importing the __main__ script
a second time as a module:
-----[foo.py]-----
class A:
    pass

import foo

print issubclass(A, foo.A)
------------------

executing "python foo.py" prints
True
False

 > [...]

Bye,
    Walter Dörwald




More information about the Python-list mailing list