when does issubclass fail?

Discussion in 'Python' started by Chris Green, Apr 16, 2004.

  1. Chris Green

    Chris Green Guest

    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 <>
    A watched process never cores.
     
    Chris Green, Apr 16, 2004
    #1
    1. Advertisements

  2. 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
     
    =?ISO-8859-1?Q?Walter_D=F6rwald?=, Apr 20, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred
    Replies:
    0
    Views:
    683
  2. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    636
    E. Robert Tisdale
    Jul 31, 2003
  3. Nathan Bullock

    How do you make issubclass work

    Nathan Bullock, Sep 11, 2004, in forum: Python
    Replies:
    4
    Views:
    811
    Alex Martelli
    Sep 11, 2004
  4. Osmo Maatta
    Replies:
    3
    Views:
    636
    Osmo Maatta
    Aug 29, 2010
  5. kj

    issubclass(dict, Mapping)

    kj, Dec 22, 2010, in forum: Python
    Replies:
    10
    Views:
    682
  6. kj
    Replies:
    5
    Views:
    617
  7. Replies:
    1
    Views:
    222
    Steven D'Aprano
    May 31, 2012
  8. lars van gemerden

    weird isinstance/issubclass behavior?

    lars van gemerden, Nov 29, 2012, in forum: Python
    Replies:
    3
    Views:
    223
    Terry Reedy
    Nov 29, 2012
Loading...