Test an object is a subclass of the type of another instance

You’d need to extract the type of obj with the type() function:

isinstance(obj2, type(obj1))

Note that the second argument is the class, the first is the instance to test. type() is returning the actual class object here, not any separate object.

issubclass() works just fine for your usecase:

issubclass(type(obj2), Item)

Demo:

>>> class Item:
...     def __init__(self,a):
...         self.a=a
... 
>>> class Sub(Item):
...     def __init__(self,a,b):
...         self.b=b
...         Item.__init__(self,a)
... 
>>> class SubSub(Sub):
...     def __init__(self,a,b,c):
...        self.c=c
...        Sub.__init__(self,a,b)
... 
>>> obj1=Item(1)
>>> obj2=Sub(1,2)
>>> obj3=SubSub(1,2,3)
>>> isinstance(obj2, type(obj1))
True
>>> issubclass(type(obj2), Item)
True

Note that if you re-defined the classes here, existing instances will not be updated to point to the new class objects. If type(obj2) doesn’t work for you, then that means that the class used to produce it is not the same you are testing with now.

You can test if this the case by testing your assumptions; validate that the classes and instances are still in sync, for example:

>>> type(obj1) is Item
True
>>> type(obj2) is Sub
True
>>> type(obj3) is SubSub
True

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)