issubclass() function in python :
Sometimes we need to quickly verify if a class is subclass of a different class or subclass of another tuple of classes of not. All classes in python are derived from a parent class known as ‘Object class’. If we derive a class from a different class, this new class is called a child class and the original class is called_ parent class or super class._
In python, issubclass() function is used to check if a class is a subclass of another class. Easier than you think, just use one single function. The syntax of the function is :
It will return one boolean value. True if class B is a subclass or derived class of class A, False otherwise. We don’t need to import any module to use this function. You can use it anywhere you want.
We can also send one tuple as the second argument to this function like below :
It will check if B is a subclass of A or B is a subclass of C etc. If B is a subclass of any of the classes defined in the tuple, it will return True. In this tutorial, we will show you how to use the issubclass() function with example.
Check if one class is a subclass of another class :
Let’s try issubclass with one single class using the first definition defined above. It will take two parameters and both of them will be a single class. We will create three different classes and test if one class is a subclass of another class :
class MainClass:
pass
class ChildClass(MainClass):
pass
class GrandChildClass(ChildClass):
pass
print(issubclass(ChildClass, MainClass))
print(issubclass(GrandChildClass, MainClass))
print(issubclass(GrandChildClass, ChildClass))
print(issubclass(ChildClass, GrandChildClass))
print(issubclass(MainClass, MainClass))
You can also download the code from here.
It will print the below output :
Explanation :
In this example, we have created three empty classes: MainClass, ChildClass and GrandChildClass.
-
MainClass is the main class or it is not derived from any other class except the default Object class.
-
_ChildClass _is the child class of MainClass. So, MainClass is the superclass of ChildClass.
-
_GrandChildClass _is the child class of ChildClass. ChildClass is the superclass of GrandChildClass.
For the output print statements :
-
The first print statement is True because ChildClass is a subclass of the MainClass.
-
The second print statement is True because GrandChildClass is also a subclass of the MainClass.
GrandChildClass is a child of ChildClass and ChildClass is a child of MainClass. So, GrandChildClass is also a subclass of the MainClass.
-
The third print statement is also True because GrandChildClass is a subclass of the ChildClass.
-
The fourth print stament is False because ChildClass is a subclass of the GrandChildClass.
-
The last print statement is True because both classes are the same.
Using issubclass to check one class is the subclass of another tuple of classes :
We can also use issubclass to check one class with another tuple of classes. Like below :
class MainClass:
pass
class ChildClass(MainClass):
pass
class GrandChildClass(ChildClass):
pass
print(issubclass(ChildClass, (MainClass, GrandChildClass)))
It will print the below output :
Here , the first parameter is a single class that we are checking if it is a subclass of any of the classes passing in a tuple as the second parameter. The output is True because ChildClass is a subclass of MainClass. Note that ChildClass is not a subclass of GrandChildClass. It will return True if the condition is True for any of the class in the tuple.