Skip to content
Related Articles

Related Articles

Improve Article

Python isinstance() method

  • Last Updated : 28 Jun, 2021

Python supports object-oriented programming and hence classes can be created using python. Sometimes, it is essential to know mid-program that which objects belong to which class. isinstance() performs this function and hence makes our programming tasks easier.

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 a TypeError if anything other than mentioned valid class type. 

Code #1 : Demonstrating the working of isinstance for native types. 


# Python 3 code to demonstrate
# working of isinstance()
# with native types
# initializing native types
test_int = 5
test_str = "GeeksforGeeks"
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_str string? : " + str(isinstance(test_str, 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, (list, str, int))))
print ("Is test_list string or tuple? : "
        + str(isinstance(test_list, (str, tuple))))


Is test_int integer? : True
Is test_int string? : False
Is test_str string? : True
Is test_list integer? : False
Is test_list list? : True
Is test_int integer or list or string? : True
Is test_list string or tuple? : False

Code #2 : Demonstrating use of isinstance() with objects 


# 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 inhertance 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


 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :