weird isinstance/issubclass behavior?

lars van gemerden lars at rational-it.com
Thu Nov 29 09:59:37 EST 2012


Hi,

I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. 

Basically if i import a class as:

    from mod1.mod2 import A

or:

    from mod0.mod1.mod2 import A

which both result in importing the same class, a call to isinstance(inst, A) in another module can have a different output. In this module

    print type(inst), A, isinstance(inst, A), issubclass(type(inst), A)

gives:

    <class 'mod0.mod1.mod2.A'> <class 'mod1.mod2.A'> False False

resp.

    <class 'mod0.mod1.mod2.A'> <class 'mod0.mod1.mod2.A'> True True

which seems somewhat logical, but in my case a strange gotcha. My question is:
Is this intended, inevitable or a bug?

Cheers, Lars

PS: this is somewhat simpler than the actual case i've encountered, and i haven't tested this exact case, but for now i hope this is enough to get some of your insight.



More information about the Python-list mailing list