Friday, September 27, 2024
Google search engine
HomeLanguagesPython isinstance() method

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.

Syntax of Python isinstance() Method

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 isinstance() 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.

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.

Examples of isinstance() Method in Python

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 if an object is an instance of a class or its derived class.

Python3




# Python 3 code to demonstrate
# working of isinstance()
# with objects
 
# declaring classes
class gfg1:
    a = 10
 
# inherited class
class gfg2(gfg1):
    string = 'Lazyroar'
 
 
# 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.

Python3




test_str = "Lazyroar"
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.

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.

Python




class Lazyroar:
    course = 'DSA'
   
    def purchase(obj):
        return obj.course
   
   
Lazyroar.purchase = classmethod(Lazyroar.purchase)
str(isinstance(Lazyroar.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)
RELATED ARTICLES

Most Popular

Recent Comments