The isinstance() function in Python is used to check whether the mentioned object is of the specified type or not.
It returns a boolean result depending on the object and type.
Syntax:
isinstance(object, type)
isinstance() Parameters
| Parameter | Condition | Description |
|---|---|---|
| object | Required | Object that is to be checked |
| type | Required | type, class or tuple of types or classes |
It returns True if the object is an instance of type otherwise, it returns False.
Python isinstance() Examples
Example 1:
>>> myList = [2, 5, 8];
>>> isinstance(myList, list)
True
>>> isinstance(myList, dict)
False
>>> isinstance(myList, set)
False
>>> isinstance(myList, (dict, list))
True
Example 2:
class myClass:
website = "pencilprogrammer.com"
def display(self):
print("Learn Programming on {}".format(self.website))
obj = myClass()
#obj.display()
print(isinstance(obj,int))
print(isinstance(obj,myClass))
False True
Example 3:
>>> n = 5
>>> isinstance(n,int)
True
>>> n = 1.4
>>> isinstance(n,float)
True