isinstance() in Python | Python isinstance() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The isinstance() function in python returns True if the specified object is of the specified type; otherwise, it will return False. It states that when we pass an object and class, or tuple of class, to the isinstance function, if the object's data type is the same as the given class, it will return True; otherwise, False.

Syntax of isinstance() in Python

The syntax of the isinstance function in Python is:

Where object can be any data structure available in python, and classinfo is a class object or group of tuples.

Tip:

Tuple is one of the four inbuilt data types available in python. The tuple is enclosed in () round brackets. It helps us to store data in it. The tuple is similar to the list in terms of indexing; the data in the tuple are unchangeable.

Parameters of isinstance() in Python

object: The object that is required to check as a part of the class or not.

classinfo: The class , type, or tuple of classes and types required to compare with the object's class.

TIP:

If classinfo is not a type or tuple of types or any other type that does not match with classinfo, then the isinstance function will raise TypeError.**

Return Type of isinstance() in Python

Return Type: bool

This function returns a boolean value, i.e., True or False.

It will return True if the object class matches the classinfo class; otherwise, False.

Example of isinstance() in Python

Code:

Output:

In the above example, a list l1 is used as an input containing integer values.

So, we consider: object: l1 classinfo: list

Now, the isinstance() function will check whether the class of the object (i.e., list) is the same as classinfo's class (i.e., list), then it will return True else False.

What is the isinstance function in Python?

The isinstance() function can be used to check the data type of the specified object matches the specified class. It states that when we pass an object and classinfo to the isinstance function, this function compares the object's class with the classinfo class(es). If any class(es) matches, it will return True; otherwise, False.

Note:

The classinfo parameter contains either a single class or a tuple of classes. In the coming section of examples, we have used a variety of examples to show how to use multiple classes to use the isinstance function.**

More Examples of isinstance() function

Example 1: Python isinstance with int and list

Code:

Output:

Example 2: Use of isinstance() with objects

Code:

Output:

Example 3: Python isinstance with string

Code:

Output:

Example 4: Python isinstance with multiple classinfo

Code:

Output:

Example 5: Python isinstance with dict

Code:

Output:

Example 6: Python isinstance class methods

Code:

Output:

Conclusion

  • The isinstance() function in python takes 2 parameters i.e., object and classinfo.
  • Isinstance function returns a bool value, True if the object class is matched with classinfo, else False.
  • In the parameter classinfo, we can pass multiple classinfo using tuples.

See Also: