By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,563 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

when does issubclass fail?

P: n/a
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.

My goal is to define a baseclass BasePoller and then have user
configured directories searched for that baseclass so I can load
modules at run time. Since I can't do absolute imports, I treat that
directory as a suitable spot to import from and do some fancy dir()
walking to find instances of my subclass.

This work fine as long as the path I search is contained within an
original element of the main init script. I'm trying to handle the
case where the directory is specified as an absolute path and can't
come up with a way to make issubclass work.

Is there anyway out of this other than defining a special attribute I
can check for the presense of?
--
Chris Green <cm*@dok.org>
A watched process never cores.
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.