Hit enter after type your search item

What is isinstance function in Python?

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

Example1 Example2 Example3

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 instance of subclasses. See the syntax below for learning 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 given type.

See the following section for more clarification of the isinstance() function by way of actual 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 third against a tuple that contains different types. Have a look at the code and output:

See online demo and code


Python isinstance

You see, the third statement also returned as True, as 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!

See online demo and code


isinstance float

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:

See online demo and code


isinstance class

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() function is the later returns true for the subclasses.

See a demonstration below:

See online demo and code


isinstance subclass

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.

This div height required for enabling the sticky sidebar