How will you check if a class is a child of another class?

Last updated 3 weeks, 5 days ago | 23 views 75     5

Tags:- Python

Python |  issubclass()

This is done by using a method called issubclass() provided by python. The method tells us if any class is a child of another class by returning true or false accordingly.

 

 

class Parent(object):
   pass   
 
class Child(Parent):
   pass   
 

print(issubclass(Child, Parent))    #True
print(issubclass(Parent, Child))    #False


 

We can also check if an object is an instance of a class by making use of isinstance() method:
 

obj1 = Child()
obj2 = Parent()

print(isinstance(obj2, Child))    #False 
print(isinstance(obj2, Parent))   #True