Python isinstance() Function | Check List, dict, int, array, etc

Python isinstance() function is used to know mid-program which objects belong to which class. like if you want to know about data type number(integer) is belongs to int class or not. For that, you have to pass the 2 value in the function.

First value and second guessing class name. See below example, if its matched then result will be True else false.

Output: True

Syntax

Parameter

  • Object: Required. An object to check part of a class or not.
  • Type: class/type/tuple of class or type, against which object is needed to be checked.

Return

  • True if the object is an instance or subclass of a class or any element.
  • False otherwise

1. Python isinstance list(Array), dict, int and

See the below Working example of isinstance() with Native Types:-

Output:

[11, 22, 33] is an instance of the list:- True
[11, 22, 33] is an instance of dict:- False
[11, 22, 33] is an instance of dict or list:- True
7 is an instance of the list:- False
7 is an instance of int:- True

2. Demonstrating use of isinstance() with objects

Output: True

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in PyCharm 2019.3 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6
Python 3.7
All Python Programs are in Python 3, so it may change its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.