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))
OutputFloat 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
# Defining Parent class
class Vehicles:
# Constructor
def __init__(vehicleType):
print('Vehicles is a ', vehicleType)
# Defining Child class
class Car(Vehicles):
# Constructor
def __init__(self):
Vehicles.__init__('Car')
# Driver's code
print(issubclass(Car, Vehicles))
print(issubclass(Car, list))
print(issubclass(Car, Car))
print(issubclass(Car, (list, Vehicles)))
OutputTrue
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
Similar Reads
numpy.issubclass_() function â Python numpy.issubclass_() function is used to determine whether a class is a subclass of a second class. Syntax : numpy.issubclass_(arg1, arg2) Parameters : arg1 : [class] Input class. True is returned if arg1 is a subclass of arg2. arg2 : [class or tuple of classes] Input class. If a tuple of classes, Tr
1 min read
Python MetaClasses The key concept of python is objects. Almost everything in python is an object, which includes functions and as well as classes. As a result, functions and classes can be passed as arguments, can exist as an instance, and so on. Above all, the concept of objects let the classes in generating other c
9 min read
Python objects A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to
2 min read
self in Python class In Python, self is a fundamental concept when working with object-oriented programming (OOP). It represents the instance of the class being used. Whenever we create an object from a class, self refers to the current object instance. It is essential for accessing attributes and methods within the cla
6 min read
python class keyword In Python, class keyword is used to create a class, which acts as a blueprint for creating objects. A class contains attributes and methods that define the characteristics of the objects created from it. This allows us to model real-world entities as objects with their own properties and behaviors.S
1 min read
Inner Class in Python Python is an Object-Oriented Programming Language, everything in Python is related to objects, methods, and properties. A class is a user-defined blueprint or a prototype, which we can use to create the objects of a class. The class is defined by using the class keyword.Example of classPython# creat
5 min read
classmethod() in Python The classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c
8 min read
Python Generics Python generics are type hints in Python that allow you to write functions and classes that can work with any data type. In this article, we will discuss Python Generics with code examples.What are Python Generics?Python generics are like hints in Python. They came out in Python 3.5 and newer versio
4 min read
Python Crash Course If you are aware of programming languages and ready to unlock the power of Python, enter the world of programming with this free Python crash course. This crash course on Python is designed for beginners to master Python's fundamentals in record time! Experienced Python developers developed this fre
7 min read
Abstract Classes in Python In Python, an abstract class is a class that cannot be instantiated on its own and is designed to be a blueprint for other classes. Abstract classes allow us to define methods that must be implemented by subclasses, ensuring a consistent interface while still allowing the subclasses to provide speci
5 min read