The issubclass()
function in Python is a built-in function that returns True
if the first argument is a subclass of the second argument. Otherwise, it returns False
. It is commonly used to check if a given class is a subclass of another class.
Parameter Values
Parameter | Description |
---|---|
class | The |
cls | The |
Return Values
The issubclass()
function returns a bool
indicating subclass status.
How to Use issubclass()
in Python
Example 1:
The issubclass()
method is used to check if a class is a subclass of another class.
class Parent:
pass
class Child(Parent):
pass
print(issubclass(Child, Parent)) # Output: True
Example 2:
It can also be used to check if a class is a subclass of a tuple of classes.
class A:
pass
class B:
pass
class C(A, B):
pass
print(issubclass(C, (A, B))) # Output: True
Example 3:
If the second argument is not a class or tuple of classes, a TypeError
is raised.
class Test:
pass
print(issubclass(Test, 123)) # Output: TypeError: issubclass() arg 2 must be a class or a tuple of classes