The Python’s isinstance()
function checks whether the object or variable is an instance of the specified class type or data type.
For example, isinstance(name, str)
checks if name
is an instance of a class str
.
Also, Solve: Python Basic Exercise and Beginners Quiz
Table of contents
How To Use isinstance() Function in Python
Let’s see the syntax first before moving to the example.
Syntax:
isinstance(object, classinfo)
Code language: Python (python)
- It takes two arguments, and both are mandatory.
- The
isinstance()
function checks if theobject
argument is an instance or subclass ofclassinfo
class argument
Using isinstance()
function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object.
- Pass object to isinstance()
Pass the variable you want to check as
object
argument to theisinstance()
. Here the object can be any class object or any variable name - Specify the Class or Type name as a
classinfo
argumentFor example,
isinstance(x, int)
to check ifx
is an instance of a classint
.classinfo
is a type name or Class name you want to check against the variable. Here you can specify data type name or Class name.
You can also pass multiple classes/types in a tuple format. For example, you can passint
,str
,list
,dict
, or any user-created class. - Execute your operation, If result is True
The
isinstance()
returns True if an object or variable is of a specified type otherwise False.
Example
Using isintance() we can verify whether a variable is a number or string. Let’s assume variable num = 90
, and you want to check whether num is an instance of an int type.
Output:
Yes
As we can see in the output, the isinstance()
returned True
because num
hold an integer value.
Note: If the classinfo
argument is not a Class, type, or tuple of types, a TypeError
exception is raised.
isinstance() With Built-In Types
As you know, Every value (variable) in Python has a type. In Python, we can use different built-in types such as int
, float
, list, tuple, strings, dictionary. Most of the time, you want to check the type of value to do some operations. In this case, isinstance()
function is useful.
Note: If we use the isinstance()
with any variable or object with a None
, it returns False
. Let see the simple example of it.
isinstance() With Multiple Classes
You can also check the instance with multiple types. Let’s say you have a variable, and you wanted to check whether it holds any numeric value or not, for example, a numeric value can be an int
or float
.
To verify whether a variable is an instance of one of the specified types, we need to mention all types in a tuple and pass it to the classInfo argument of isinstance()
.
Example
isinstance() With Python Class
The isinstance()
works as a comparison operator, and it compares the object with the specified class type.
You can verify if the emp
object is an instance of a user-defined class Employee using the isinstance()
function. It must return True.
isinstance() function With Inheritance
The object of the subclass type is also a type of parent class. For example, If Car is a subclass of a Vehicle, then the object of Car can be referred to by either Car or Vehicle. In this case, the isinstance(carObject, Vehicle)
will return True
.
The isinstance()
function works on the principle of the is-a relationship. The concept of an is-a relationship is based on class inheritance.
The instance()
returns True if the classinfo argument of the instance() is the object’s class’s parent class.
To demonstrate this, I have created two classes, Developer and PythonDeveoper. Here PythonDeveoper is a sub-class of a Developer class.
Note: The isinstance()
function is beneficial for casting objects at runtime because once you get to know the given class is a subclass of a parent class, you can do casting appropriately if required.
isinstance with Python list
As you know, a Python list is used to store multiple values at the same time. These values can be of any data type like numbers, strings, or any Class objects.
In this section, we will test the following operations with the Python list using the isinstance()
function:
- Checking if an object is of type
list
in python. - Check if an element of a list is a list.
- Verify if elements of a list are numbers or strings.
- Python check if all elements of a list are the same type
Checking if an object is an instance of a list type
Check if an element of a list is a nested list
To check if one of the elements in the list is itself a list. For example, you have the following list, Use the isinstance()
to verify if the list contains a nested list
sampleList = ['Emma', 'Stevan', ['Jordan', 'Donald', 'Sam']]
Code language: Python (python)
Iterate a list and verify each element’s class, and if it a list type, we can say that the list contains a nested list.
Check if elements of a list are numbers or strings
Check each element’s type with multiple numeric types such as int
, float
, and complex
using the isinstance()
function.
To find all string variables, Check each element’s type with str
type.
Next steps
Let me know your comments and feedback in the section below.
Solve: