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

Parameters:

Python issubclass() function parameters
ParameterConditionDescription
classRequiredClass that needs to be checked.
classinfoRequiredClass
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

Output

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.

Leave a Reply