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

23,855

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
Share:
23,855

Related videos on Youtube

A-P
Author by

A-P

Updated on December 15, 2020

Comments

  • A-P
    A-P over 3 years

    I have this code:

    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)
    

    Now I want to check if obj2 and obj3 are instances of classes that are subclasses of obj1 as well as simply subclasses of of Item.

    Here is what I understand, I know that I can use the isinstance() to find if obj2 is Sub. and I know that I can use issubclass(Sub, Item). But let's say I didn't know what class obj2 was.

    I tried using issubclass(type(obj2),Item) but that doesn't work, because type() returns a separate object that I don't really understand the workings of. And this is just one problem, although I figure that the answer to this question will help me solve some of the other problems that I am having.

    Also I have tried using some of the special attributes to do this such as __class__ but I can't figure out how to do that either.

  • A-P
    A-P over 9 years
    Okay, that doesn't work. What exactly does the type() function return, as I understand it returns a separate object that I don't think can be used in this setting.
  • Martijn Pieters
    Martijn Pieters over 9 years
    @A-P: Either you got some objects muddled up (importing vs local definitions maybe?) but type() does not produce a separate object. If it does then you didn't create that instance from the class you are testing with.
  • Martijn Pieters
    Martijn Pieters over 9 years
    @A-P: If you re-defined the classes but didn't recreate the instances, then those instances will still be referring to the old class definitions. It sounds like that is the case for you here.
  • A-P
    A-P over 9 years
    Okay yeah, I think it was some weird definition problems I was having because once I reset the shell and put everything in again it worked.
  • Martijn Pieters
    Martijn Pieters over 5 years
    @selftaught91: that doesn't tell me much, I can't help you if you don't tell me what you tried and what happened instead.