What is isinstance function in Python?

Python isinstance() function is used to test the type of an object. For example, testing the type of list, tuple, dictionary, integer, float, etc.

The type() function can also be used for testing the object type, however, the recommended way is isinstance() function officially.

One of the reasons is, the isinstance() also returns as True for instances of subclasses.

See the syntax below to learn how it works and the last example to see the difference between type() and isinstance() functions.

Syntax of isinstance() function

isinstance(object, classinfo)

A few important point about the Python isinstance() function()

  • The isinstance requires two parameters.
  • The object is the required parameter. This is the object that you want to test. For example, it can be a list, integer or float variables, tuple, dictionary etc.
  • The classinfo is also a required parameter. This can be a type, class or tuple that contains classes and types.
  • The return value of the isinstance method is a Boolean i.e. True or False.
  • It returns True if the object argument is the instance or subclass of the classinfo.
  • It returns False if the object argument is not the object of the given type.

See the following section for more clarification of the isinstance() function by way of examples.

An example of checking a list by isinstance() function

In this example, a list is created and tested by using the isinstance() function.

The first statement is checked against the list type, the second against the dict, and the third against a tuple that contains different types.

Have a look at the code and output:

#The isinstance() demo

lst_inst = [7, 11, 17, 27]

print("isinstance(lst_inst, list) = " ,isinstance(lst_inst, list))

print("isinstance(lst_inst, dict) = ", isinstance(lst_inst, dict))

#Testing against tuple

print("isinstance(lst_inst, (dict,int,float, list))", isinstance(lst_inst, (dict,int,float, list)))

Output:

Python isinstance

You can see that the third statement also returned as True, as the tuple contains the list type.

An example of float type

Similarly, you may test the variable types like float, int, string, etc by using the isinstance() function, as such these are also objects in Python.

In the following example, a float variable is created and its type is tested by using the isinstance Python function.

The isinstance() function is used in the if statement; if it returns True the message will be displayed whether it’s a float or not!

Python program:

#The isinstance() demo

a_flt = 37.77

if isinstance(a_flt, float):

    print ("This is a float value")

else:

    print ("Not a float")

Result:

This is a float value

A custom class example with isinstance

For this example, a class is created with “no statement” to execute except the pass statement.

An instance of this class is created and checked by using the isinstance() function.

See the code and output below:

#The isinstance() demo with a class

class tstinstance():

    pass

ins_cls = tstinstance()

#Testing class with tuple

print("isinstance(ins_cls, (tuple,str, float, tstinstance, list))", isinstance(ins_cls, (tuple,str, float, tstinstance, list)))

print("isinstance(ins_cls, tstinstance) = " ,isinstance(ins_cls, tstinstance))

Output:

isinstance(ins_cls, (tuple,str, float, tstinstance, list)) True
isinstance(ins_cls, tstinstance) = True

The output shows True for both cases as class exists in the tuple as well.

The isinstance with subclass – the difference between Type and isinstance

For this example, a main CarBrand class is created and a subclass LatModel is also created.

As mentioned in the above section, one of the differences between the type() and isinstance() functions is the latter returns true for the subclasses.

#The isinstance() demo with a subclass

#Main class

class CarBrand():

    pass

#Subclass

class LatModal(CarBrand):

    pass

#instance of subclass

car = LatModal()

#checking the main class

print("isinstance(car,CarBrand) Output = ", isinstance(car,CarBrand))

#checking the subclass

print("isinstance(car,LatModal) Output = ", isinstance(car,LatModal))


#Testing with type() function

print("type(car) == LatModal  = ", type(car) == LatModal)

print("type(car) == CarBrand  = ", type(car) == CarBrand)

Output:

isinstance(car,CarBrand) Output = True
isinstance(car,LatModal) Output = True
type(car) == LatModal = True
type(car) == CarBrand = False

In the output, it is clear that the isinstance() returned True for both the main and subclass. On the other hand, the type() function returned True for the subclass, however, False for the main class.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!