weird isinstance/issubclass behavior?
tjreedy at udel.edu
Thu Nov 29 22:00:48 CET 2012
On 11/29/2012 9:59 AM, lars van gemerden wrote:
> 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
> from mod0.mod1.mod2 import A
> which both result in importing the same class,
As other said, both import the same abstract class but create two
different concrete class objects.
> a call to isinstance(inst, A) in another module can have a different
> In this module
> print type(inst), A, isinstance(inst, A), issubclass(type(inst), A)
> <class 'mod0.mod1.mod2.A'> <class 'mod1.mod2.A'> False False
Add print id(type(inst)), id(A) and you will see that they are different
objects. The isinstance and issubclass calls compare the classes by
identity (the default meaning of ==) and so False, False are correct.
Terry Jan Reedy
More information about the Python-list