The isinstance() function in Python is used to check whether the mentioned object is of the specified type or not. It returns True if yes, otherwise false.
Syntax:
isinstance(object, type)
isinstance() Parameters
object – object that is to be checked.
type – type, class or tuple of types or classes.
Let’s check out some examples.
Python isinstance() Example
Exampe 1:
myList = [2, 5, 8]; print(isinstance(myList, list)) print(isinstance(myList, dict)) print(isinstance(myList, set)) #object = myList #type = tuple of types print(isinstance(myList, (dict, list)))
True False False 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 print(isinstance(n,int)) n = 1.4 print(isinstance(n,float))
True True
If you have any doubts or suggestion then comment below.