Python isinstance() Function Tutorial

In this section, we will learn what the isinstance() function is and how to use it in Python.

What is Python isinstance() Function?

The isinstance() function is used to see if an object is of a specific type.

Python isinstance() Function Syntax:

isinstance(object, type)

isinstance() Function Parameters:

The function takes two arguments:

  • The first argument is the object we want to check its type.
  • The second argument of the function is the data type we want to check the object with. For example, the value of this argument can be `str` or `int` or `float` or `list` etc. Note that the value of the second argument could be a tuple object with a range of data types too! For example, (int, str, float). This means the function now compares the first argument (the object) with the elements of the tuple object and if one of them matched the data type of the specified object, then the result of the function becomes True.

isinstance() Function Return Value:

The return value of this function is of type boolean.

If the object was of the specified type, then the return value of this function becomes True.

Otherwise, if the object was not of the specified type (or the range of types we put in a tuple object), then the return value of the function becomes False.

Example: using isinstance() function in Python

class Parent: 

    def sayHi(self):
        print("Hello from the parent class")

class Child (Parent):

    def sayHi(self):
        print("Hello from the Child class")

child = Child()

res = isinstance(child, Child)
res2 = isinstance(child, Parent)

val = 5.33

res3 = isinstance(val, (int, str, float,bool))





Top Technologies