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

issubclass(class, classinfo)

Parameters:

ParameterConditionDescription
classRequiredA class that needs to be checked.
classinfoRequiredClass
Type
A Tuple of Classes or Types.

Return Value: True if the class is the subclass of the specified class or type or any element of the tuple. False otherwise.

Python issubclass() Example

Recommended: Python Inheritance

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:

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 the list.

issubclass(Dog,(Animal, list) returns True because Dog class is a subclass of one of the elements of the tuple 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.

Leave a Reply