Python isinstance()

The isinstance() function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).

Example


isinstance() Syntax

The syntax of isinstance() is:

isinstance(object, classinfo)

isinstance() Parameters

isinstance() takes two parameters:

  • object - object to be checked
  • classinfo - class, type, or tuple of classes and types

isinstance Return Value

isinstance() returns:

  • True if the object is an instance or subclass of a class or any element of the tuple
  • False otherwise

If classinfo is not a type or tuple of types, a TypeError exception is raised.


Example 1: How isinstance() works?

Output

True
False
True

Example 2: Working of isinstance() with Native Types

Output

[1, 2, 3] instance of list? True
[1, 2, 3] instance of dict? False
[1, 2, 3] instance of dict or list? True
5 instance of list? False
5 instance of int? True