Python issubclass()

Python issubclass() builtin function is used to check if given class is a subclass of other class.

In this tutorial, we will learn about the syntax of Python issubclass() function, and learn how to use this function with the help of examples.

Syntax

The syntax of issubclass() function to check if x is a subclass of y is

issubclass(x, y)

where

ParameterRequired/OptionalDescription
xRequiredA class.
yRequiredA class.

Returns

The function returns boolean value.

ADVERTISEMENT

Example

In this example, we define three classes A, B and C such that B is a subclass of A. Using issubclass() function, we programmatically check if B is a subclass of A, and if C is a subclass of A.

Python Program

class A:
    name = 'A'
    
class B(A):
    name = 'B'
    
class C:
    name = 'C'
     
print('Is B subclass of A :', issubclass(B, A))
print('Is C subclass of A :', issubclass(C, A))
Try Online

Output

Is B subclass of A : True
Is C subclass of A : False

Conclusion

In this Python Tutorial, we have learnt the syntax of Python issubclass() builtin function, and also learned how to use this function, with the help of Python example programs.