Python issubclass()
Last Updated :
29 Nov, 2023
We know that inheritance is one of the building blocks of the Object-Oriented Programming concept. One class can derive or inherit the properties from some other class. It also provides the reusability of code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
Python issubclass() Function Syntax
Syntax: issubclass( object, classinfo )
Parameters:
- Object: class to be checked
- classinfo: class, types or a tuple of classes and types
Return Type: True if object is subclass of a class, or any element of the tuple, otherwise False.
issubclass() Function in Python
Python issubclass() is a built-in function used to check if a class is a subclass of another class or not. This function returns True if the given class is the subclass of the given class else it returns False. We can determine class inheritance in Python by using issubclass().
Check Subclass of Built-In Functions
In this example, we will see how we can check the sub-classes of built-in functions.
Python3
print ( "Float is the subclass of str:" , issubclass ( float , str ))
print ( "Bool is the subclass of int:" , issubclass ( bool , int ))
print ( "int is the subclass of float:" , issubclass ( int , float ))
import collections
print ( 'collections.defaultdict is the subclass of dict: ' , issubclass (collections.defaultdict, dict ))
|
Output
Float is the subclass of str: False
Bool is the subclass of int: True
int is the subclass of float: False
collections.defaultdict is the subclass of dict: True
How Python issubclass() works?
To determine class inheritance in Python, we define multiple classes representing the phenomenon of Inheritance. Then, for a particular class, we check whether it is the subclass of the mentioned base class or not using the issubclass
function.
Python3
class Vehicles:
def __init__(vehicleType):
print ( 'Vehicles is a ' , vehicleType)
class Car(Vehicles):
def __init__( self ):
Vehicles.__init__( 'Car' )
print ( issubclass (Car, Vehicles))
print ( issubclass (Car, list ))
print ( issubclass (Car, Car))
print ( issubclass (Car, ( list , Vehicles)))
|
Output
True
False
True
True
Python issubclass not working TypeError
In this example, we are showing how issubclass() gives TypeError.
Python3
print ( issubclass ( 1 , int ))
|
Output:
Traceback (most recent call last):
File "c:\Users\DELL\ranking script", line 36, in <module>
print(issubclass( 1, int ))
TypeError: issubclass() arg 1 must be a class
Solution for Python issubclass arg 1 must be a class TypeError, one should always pass a class instead of a non class type argument.
Python3
print ( issubclass ( type ( 1 ), int ) )
print ( issubclass ( type ( 'geeksforgeeks' ), str ) )
|
Note: Don’t get confused between isinstance() and issubclass() as both these method are quite similar. However, the name itself explain the differences. isinstance() checks whether or not the object is an instance or subclass of the classinfo. Whereas, issubclass() only check whether it is a subclass of classinfo or not (not check for object relation).
Related Articles
Share your thoughts in the comments
Please Login to comment...