Python issubclass() is a built-in function that is used to check whether a class is a subclass of another class or not. It returns True
if the specified class is a subclass of another class else returns False
.
In python Inheritance, a class that derives or inherits from another class is called a subclass and the class from which it is derived is called the superclass.
Python issubclass() Syntax
1 | issubclass(class, classinfo) |
Parameters:
Parameter | Condition | Description |
---|---|---|
class | Required | Class that needs to be checked. |
classinfo | Required | Class Type A Tuple of Classes or Types. |
Return Value: True
if the class is the subclass of specified class or type or any element of the tuple. False
otherwise.
Python issubclass() Example
Recommended: Python Inheritance
1 2 3 4 5 6 7 8 9 10 11 | class Animal: def __init__(self): print("Animal") class Dog(Animal): def __init__(self): print(Dog) print(issubclass(Dog, Animal)) print(issubclass(Dog, list)) print(issubclass(Dog,(Animal, list))) |
Output
1 2 3 | True False True |
issubclass(Dog, Animal)
returns True
because Dog
class is deriving from Animal
class, so it is a subclass of Animal
.
issubclass(Dog, list)
returns False
because Dog
is not a subclass of list.
issubclass(Dog,(Animal, list)
returns True
because Dog
class is a subclass of one of the element of the tupple i.e Animal
.
Difference Between isintance() and issubclass()
Python isinstance(object, class) checks whether an object is an instance of class
or not. It also checks True
if the object
is an instance of any subclass of the specified class
parameter.
whereas issubclass(class, classinfo)
only checks whether the particular class
is a subclass of the specified class (classinfo
) or not.