Python isinstance() method
Python isinstance() function returns True if the object is specified types, and it will not match then return False.
Syntax:
isinstance(obj, class)Parameters :
- obj : The object that need to be checked as a part of class or not.
- class : class/type/tuple of class or type, against which object is needed to be checked.
Returns : True, if object belongs to the given class/type if single class is passed or any of the class/type if tuple of class/type is passed, else returns False. Raises
TypeError: if anything other than mentioned valid class type.
Example: Python isinstance()
Example 1: Python isinstance with int and list
Python3
# Python 3 code to demonstrate # working of isinstance() # with native types # 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 )))) |
Output:
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
Example 2: Demonstrating the use of isinstance() with objects
Python3
# Python 3 code to demonstrate # working of isinstance() # with objects # declaring classes class gfg1: a = 10 # inherited class class gfg2(gfg1): string = 'GeeksforGeeks' # 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))) |
Output:
Is obj1 instance of gfg1? : True Is obj2 instance of gfg2? : True Is obj1 instance of gfg2? : False Is obj2 instance of gfg1? : True
Example 3: Python isinstance array
Python3
test_list = [ 1 , 2 , 3 ] print ( "Is test_list list? : " + str ( isinstance (test_list, list ))) |
Output:
Is test_list list? : True
Example 4: Python isinstance string
Python3
test_str = "GeeksforGeeks" print ( "Is test_str string? : " + str ( isinstance (test_str, str ))) |
Output:
Is test_str string? : True
Example 5: Python isinstance dict
Python3
test_dict = { "apple" : 1 , "Ball" : 2 } print ( "Is test_str dictionary? : " + str ( isinstance (test_dict, dict ))) |
Output:
Is test_str dictionary? : True
Example 6: Python isinstace class methods
Python
class geeks: course = 'DSA' def purchase(obj): return obj.course geeks.purchase = classmethod (geeks.purchase) str ( isinstance (geeks.purchase(), str )) |
Output:
True
Please Login to comment...