Updated April 14, 2023
Introduction to Python isinstance
This function checks if the object is an instance of the class or in for a class. This function allows you to check the object against the subclass. This function always returns a boolean value. The object we are passing into the isinstance method of the same type then the isinstance method will true; otherwise, it will return false. Let’s suppose we have a number 25, and we want to check whether it is an integer type; then, we can use the isinstance function, and in it will return true, as 25 is int.
Syntax:
isinstance(object, classinfo)
Examples of Python isinstance
Examples of python isinstance are given below:
Example #1
Code:
lst = [1,2,3,'a']
print(isinstance(lst, list))
Output:
We have created a list and stored it in a variable lst. We have lst as an object in isinstance function and object type as a list. They match, and isinstance returns true.
lst = [1,2,3,'a']
print(isinstance(lst, tuple))
Output:
In the above example, this time, we have passed object type as a tuple and isinstance return false, as lst is not tuple type. Isinstance method can be used to check variables with multiple data types.
Example #2
Code:
lst = [1,2,3,'a']
dist = {'A':[1,2,3], 'B':['a','b','c']}
print(isinstance(dist, tuple))
Output:
We have made a dictionary, and it consists of two sets. Now, if I pass this dictionary to isinstance and check with tuple class, it will return false. As we know, a dictionary is a distant object type.
lst = [1,2,3,'a']
dist = {'A':[1,2,3], 'B':['a','b','c']}
print(isinstance(dist, (tuple, dict)))
Output:
Now this time, we have passed both tuple and dict into object type, and isinstance will return true this time. The Isinstance function is very useful when we are using different classes and writing more function programs than we will be using this method so much.
Example #3
Code:
class test1:
a = 1
class test2:
b = 2
t1 = test1()
t2 = test2()
print(isinstance(t1, test1))
Output:
In the above program, we have created two classes test1 and test2 and created two variables, a and b, with values 1 and 2. t1 is the instance of the class test1, and t2 is the instance of class test2. Now we are checking t1 objects with the object type of test1. Isinstance will be true because it belongs to the same instance.
Example #4
Code:
a = 5
print('Is a is int ?: ', isinstance(a, int))
b = 2.5
print('Is b is floaf?: ', isinstance(b, float))
c = 'lol'
print('Is c is String: ', isinstance(c, str))
e = (1, 2, 3)
print('Is e is tuple?: ', isinstance(e, tuple))
f = []
print('Is f is list?:', isinstance(f, list))
g = {}
print('Is g is list?:', isinstance(g, dict))
Output:
In the above example, we have created the variable of different data types, and we have checked every variable in the isinstance method, and the following outputs are generated.
Example #5
Code:
def multiplication(p1,p2):
if isinstance(p1,(int)) and isinstance(p2, (int)):
return f'Params Ok! Result: {p1*p2}'
else:
return 'Params must be type of (int)'
print(multiplication(2,'f'))
Output:
In the above program, we have created a method multiplication that is taking two parameters p1 and p2. Then we are using isinstance method two checks whether both the parameters are of int type or not. If any of the parameters is not integer type, then isinstance will return false, and we will display an error message. We have used the print function to call the method and passed 1 integer, and the second parameter as character and function has returned an error message as expected.
Now we are passing both the parameters as an integer, and this time we get the multiplication of both the numbers.
Code:
def multiplication(p1,p2):
if isinstance(p1,(int)) and isinstance(p2, (int)):
return f'Params Ok! Result: {p1*p2}'
else:
return 'Params must be type of (int)'
print(multiplication(2,3))
Output:
Difference between isinstance and Type Function
The purpose of both functions is the same to check the data type of the parameter, but implementation is different with different use cases. Type function will return the object’s data type, while the isinstance will return value based on the object and data type we pass.
Suppose you just want to know the data type of the parameter, then the type function is a good choice, and if we want to check if the data type of pass is the same as the data type of the second parameter, then it isinstance function is recommended. It will return a boolean value.
Conclusion
Isinstance method is a very useful functionality in the python program for functional programming because many times, we need to check that the object is the instance of the class or not, and this function returns the boolean output. The object we are passing into the isinstance method of the same type then the isinstance method will true; otherwise, it will return false.
Recommended Articles
We hope that this EDUCBA information on “Python isinstance” was beneficial to you. You can view EDUCBA’s recommended articles for more information.