We know that inheritance is one of the building blocks of Object-Oriented Programming concept. It is the capability of one class to 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.
issubclass() in Python
Python issubclass() is 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 given class else it returns False.
Syntax: issubclass( object, classinfo )
- 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.
For issubclass python we are defining multiple classes and representing the phenomenon of Inheritance, then for a particular class we are checking whether it is the subclass for the mentioned base class or not.
True False True True
Python issubclass not working TypeError
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.
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).
Please Login to comment...