Python issubclass()
is a built-in function that returns true
if a class supplied as the first argument is the subclass of another class supplied as the second argument, else it returns false
.
In Python, a class is considered as the subclass of itself.
Python issubclass() Syntax
issubclass(class, classinfo)
Python issubclass()
takes two parameters.
- class (required) – a class that is to be checked
- classinfo (required) – a class, type or a tuple of classes or types
Here classinfo
can be a tuple of class objects, in which case every entry in classinfo is checked. In any other case, a TypeError
exception is raised.
So, the issubclass(class, classinfo)
function will return true
if and only if the class is a subclass of classinfo
, else it will return false
.
Python issubclass() Example
>>> class Car:
pass
>>> class Buggati(Car):
pass
>>> issubclass(Buggati, Car)
True
>>> issubclass(Car, Buggati)
False
>>> #checking if a class is subclass of itself
>>> issubclass(Car, Car)
True
Seems pretty much same as Python isinstance()
function. So what is the difference?
Difference between Python issubclass() and isinstance()
The difference is basic.
isinstance(object, classinfo) basically checks whether or not the object is an instance or subclass of classinfo
.
Whereas, issubclass(class, classinfo)
checks whether or not a class is subclass of classinfo
.