Examples of isinstance() Method in Python
We can provide a single class type or a Python tuple of classes to the instance() method. In the case of a tuple, the instance() method checks for all the elements in the tuple and returns True if the object is an instance of any one of the elements of the tuple, else it returns False. Let us see a few examples of the Python instance() method.
Python isinstance with Int and List
In this example, we will see how the isinstance() method work with an Integer datatype and with Python List. We check if the integer and the list are an instance of an Integer or of String type.
Python3
# initializing native types test_int = 5 test_list = [ 1 , 2 , 3 ] # testing with isinstance print ( "Is test_int integer? : " + str ( isinstance (test_int, int ))) print ( "Is test_int string? : " + str ( isinstance (test_int, str ))) print ( "Is test_list integer? : " + str ( isinstance (test_list, int ))) print ( "Is test_list list? : " + str ( isinstance (test_list, list ))) # testing with tuple print ( "Is test_int integer or list or string? : " + str ( isinstance (test_int, ( int , list , str )))) |
Is test_int integer? : True Is test_int string? : False Is test_list integer? : False Is test_list list? : True Is test_int integer or list or string? : True
Demonstrating the use of isinstance() with Objects
In this example, we will check an object’s class in Python i.e, if an object is an instance of a class or its derived class.
Python3
# declaring classes class gfg1: a = 10 # inherited class class gfg2(gfg1): string = 'w3wiki' # initializing objects obj1 = gfg1() obj2 = gfg2() # checking instances print ( "Is obj1 instance of gfg1? : " + str ( isinstance (obj1, gfg1))) print ( "Is obj2 instance of gfg2? : " + str ( isinstance (obj2, gfg2))) print ( "Is obj1 instance of gfg2? : " + str ( isinstance (obj1, gfg2))) # check inheritance case # return true print ( "Is obj2 instance of gfg1? : " + str ( isinstance (obj2, gfg1))) |
Is obj1 instance of gfg1? : True Is obj2 instance of gfg2? : True Is obj1 instance of gfg2? : False Is obj2 instance of gfg1? : True
Python isinstance() with String
In this example, we will use the isinstance() function with a Python String and check an object’s class in Python.
Python3
test_str = "w3wiki" print ( "Is test_str string? : " + str ( isinstance (test_str, str ))) |
Is test_str string? : True
Python isinstance() with Dictionary
Python isinstance() method also works with a dictionary object and check an object’s class in Python.
Python3
test_dict = { "apple" : 1 , "Ball" : 2 } print ( "Is test_str dictionary? : " + str ( isinstance (test_dict, dict ))) |
Is test_str dictionary? : True
Python isinstance with Class Methods
In this example, we use the isinstance() method to check the value returned by a class function with a specified type and check an object’s class in Python.
Python3
class geeks: course = 'DSA' def purchase(obj): return obj.course geeks.purchase = classmethod (geeks.purchase) str ( isinstance (geeks.purchase(), str )) |
Output
True
Python isinstance() method
Python isinstance() function returns True if the object is of specified types, and if it does not match then returns False. In this article we will see how isinstance() method works in Python
Example
Input: isinstance([1, 2, 3], list)
Output: True
Explanation: The first parameter passed is of list type.
Input: isinstance(10, str)
Output: False
Explanation: The first parameter, 10 is an integer and not a string.