Python isinstance() Function
The Python isinstance() function is used to check whether the specified object belongs to the specified type or not. It returns true when the specified object belongs to the specified type, else returns false.
Syntax
isinstance(object, type)
Parameters
object |
Required. Specify the object which need to be checked. |
type |
Required. Specify object type or class, or tuple of object types or classes. |
Example:
In the example below, isinstance() function is used to check the object called MyList is a list or not.
MyList = [1, 2, 3] x = isinstance(MyList, list) print(x)
The output of the above code will be:
True
Example:
Instead of specifying one object type, a tuple of object types or classes can be used. In the example below, isinstance() function is used to check the object called MyNumber belongs to tuple of types or not.
MyNumber = 5+5j x = isinstance(MyNumber, (int, float, str)) print(x) MyNumber = 5+5j x = isinstance(MyNumber, (int, float, str, complex)) print(x)
The output of the above code will be:
False True
❮ Python Built-in Functions