Python issubclass() Function



The python issubclass() function is a built-in function used for verifying whether a given class is a subclass of specified class. In object-oriented programming, a subclass is a class that extends the functionality of another class, referred to as its superclass or parent class.

If the given class is a subclass of the specified class, the issubclass() function returns True, otherwise, False. Note that a class is also a subclass of itself.

Syntax

The syntax of the Python issubclass() function is as follows −

issubclass(object, subclass)

Parameters

Following are the parameters of the Python issubclass() function −

  • object − This parameter specifies an object whose attribute needs to be searched.

  • subclass − This parameter represents a class object, or a tuple of class object.

Return Value

The Python issubclass() function returns a Boolean value (either True or False).

Examples

In this section, we will understand the working of issubclass() function with the help of examples −

Example 1

The following is an example of the Python issubclass() function. In this, we have created a class along with its subclass and, trying to verify whether the class named "ClassChild" is subclass or not.

class ClassParent:
   pass

class ClassChild(ClassParent):
   pass

output = issubclass(ClassChild, ClassParent)
print("Is ClassChild is child class of ClassParent:", output) 

On executing the above program, the following output is generated −

Is ClassChild is child class of ClassParent: True

Example 2

The issubclass() function also works with multiple inheritance. Here, we have defined two parent classes along with a child class. Then, tried to check if the class "ClassTata" is a child class of both defined classes.

class ClassVehicle:
   pass

class ClassCar:
   pass

class ClassTata(ClassVehicle, ClassCar):
   pass

checkOne = issubclass(ClassTata, ClassVehicle)
checkTwo = issubclass(ClassTata, ClassCar)
print("Is ClassTata is child class of ClassVehicle:", checkOne) 
print("Is ClassTata is child class of ClassCar:", checkTwo)

The following is the output obtained by executing the above program −

Is ClassTata is child class of ClassVehicle: True
Is ClassTata is child class of ClassCar: True

Example 3

In Python, all classes including "int" are subclasses of the "object" class. To verify this fact, we have used the issubclass() function in the below example code.

res = issubclass(int, object)
print("Is int is subclass of object class:", res)

The following output is obtained by executing the above program -

Is int is subclass of object class: True

Example 4

The issubclass() function also accepts arguments in the form of a tuple. In this example, we are passing a tuple of classes to check whether the class named "ClassTata" is a subclass of specified objects or not.

class ClassVehicle:
   pass

class ClassCar(ClassVehicle):
   pass

class ClassTata(ClassCar):
   pass

checker = issubclass(ClassTata, (ClassCar, ClassVehicle))
print("Is ClassTata is child class:", checker)

The above program, on executing, displays the following output −

Is ClassTata is child class: True
python_built_in_functions.htm
Advertisements