Python isinstance() and issubclass() functions :

Class and instance are object oriented concepts that most of us are familiar with. All classes in python are derived from a base class called Object class. We can also create a class deriving any other class. This new class is called a childclass or subclass of the main class, and the main class is called parent or superclass.

The instance of a class is different than the subclass. We can create as many instance of a class as we like. You can think class as a ‘blueprint’ and the instances as the objects created using the blueprint. For example, if we have a class ‘Car’ with an attribute ‘color’, we can create different instance or objects of ‘Car’ class with different values for this attribute. We can create objects or instances of ‘Car’ with different ‘color’ like ‘blue’,’red’,’green’ etc.

Sometimes we need to check if an object is an instance of a class or not, and also if a class is a subclass of a different class or not. In python we can do it pretty easily using two functions called ‘isinstance()’ and ‘issubclass()’.

Most of us get confused with isinstance() and issubclass() functions in python. isinstance() is used to check if an object is an instance of a certain class or any of its subclass. Again, issubclass() is used to check if a class type is the subclass of a different class.

In this tutorial, I will show you how to use these methods with examples.

 

isinstance(object, classinfo)

This method is used to check if an object is an instance of a class. It takes two parameters. The first one is the object to test and the second one is a single class or a tuple of classes. It will check if the first parameter object is an instance of class classinfo. If the second parameter is a tuple of classes, it will check if the first object is an instance of any of the classes in the tuple.

 

issubclass(class, classinfo)

This method is used to check if a class is a subclass of a different class. It will check if class is a subclass of another class classinfo or a tuple of classes.
Note that for both methods, the second parameter is a single class or a tuple of classes.

 

Example of isinstance() and issubclass() :

Let’s try to understand both functions with a simple example :

python-isinstance-issubclass

It will print the below output :

python-isinstance-issubclass

python isinstance issubclass explanation

 

Explanation :

Let’s try to understand the above print statements :

1. print(isinstance(child, MainClass)) : child object is an instance of child class of MainClass i.e. ChildClass
2. print(isinstance(child, ChildClass)) : child object is an instance of class ChildClass
3. print(issubclass(ChildClass, ChildClass)) : same ChildClass.
4. print(issubclass(ChildClass, MainClass)) : ChildClass is sub class of MainClass.
5. print(issubclass(ChildClass, (MainClass, GrandChildClass))) : ChildClass is sub class of MainClass. Note that it is not a sub class of GrandChildClass.
6. print(isinstance(child, (MainClass, GrandChildClass))) : child object is an instance of child class of MainClass

 

You might also like :

Categories: python

Leave a Reply

Your email address will not be published. Required fields are marked *