Open In App

Python isinstance() method

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Python isinstance() Function Syntax

The isinstance() method in Python has the following syntax:

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.

TypeError: if anything other than mentioned valid class type. 

How instance() Function works in Python?

In Python, the instance() method works like a comparison operator. It takes two arguments, one is a Python object and the other is a class type. It compares the object with a specified type of class or a subclass and returns a boolean value, that is either True or False.

Python3




numbers = [1, 2, 3, 4, 2, 5]
 
# Check if 'numbers' is an instance of a list
result = isinstance(numbers, list)
 
if result:
    print("The variable 'numbers' is an instance of a list.")
else:
    print("The variable 'numbers' is not an instance of a list.")


Output

The variable 'numbers' is an instance of a list.


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))))


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


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 = '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


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 = "GeeksforGeeks"
print ("Is test_str string? : " + str(isinstance(test_str, str)))


Output

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)))


Output

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

Difference between isinstance() and type() Methods in Python

The following table demonstrates the differences between the isinstance() method and the type() method in Python.

isinstance()

type()

Syntax: isinstance(object, class) Syntax: type(object)

It checks if an object is of a specific class type

It returns the class type of an object

It can check if the object belongs to a class and its subclasses

It cannot deal with inheritance

It is faster as compared to type() It is slower than isinstance()
It returns either True or False It returns the type of the object
It can check for multiple classes at a time It cannot do this
Example: isinstance(10, (int, str)) Example: type(10)


Last Updated : 16 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads