Isinstance () function in Python

The built-in isinstance () function in Python checks whether an object is an instance or a subclass of Classinfo.

The built-in isinstance () function checks whether an object (first parameter) is an instance or a subclass of Classinfo (the second parameter).

The isinstance () function syntax in Python

 isinstance(object, classinfo) 

Parameters of isinstance function ()

The isinstance () function has 2 parameters:

  1. object : object to check
  2. classinfo : class, type, or tuple

Value returned from isinstance ()

Isinstance () function returns:

  1. True if the object is an instance or a subclass of class or any element of the data set.
  2. False if vice versa.

For example, if the classinfo parameter is a tuple, this function will return True if the object is a type in tuple.

Example: How does isinstance () function work?

 class Foo: a = 5 fooInstance = Foo() print(isinstance(fooInstance, Foo)) print(isinstance(fooInstance, (list, tuple))) print(isinstance(fooInstance, (list, tuple, Foo))) 

Run the program, the result is:

 True False True 

See also: Built-in Python functions

3.5 ★ | 2 Vote