Using the isinstance() function in Python 3

Last updated on November 3, 2021 Pennywise Loading... Post a comment

The isinstance() function in Python is used to check whether an object is an instance of a particular type.

Syntax:

isinstance(input_object, type)

Where:

  • input_object: The object you want to check type
  • type: A type, or a tuple of types

The function returns True if the type of the provided object matches the given type, otherwise False.

Example 1

a = 12
b = [1, 2, 3]
c = "KindaCode.com"

print(isinstance(a, int))
print(isinstance(b, list))
print(isinstance(c, bool))

Output:

True
True
False

Example 2

In this example, we will pass a tuple of types to the isinstance function to check if an object’s type is among them:

t = 1.23
print(isinstance(t, (int, float, bool, str)))

Output:

true

Further reading

You can also check out our Python category page for the latest tutorials and examples.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

You May Also Like