Table of Contents
In this tutorial, we will learn about the Python isinstance() function with the help of examples.
The isinstance()
function checks if the object (first argument) is an instance or subclass of classinfo class (second argument).
Example
numbers = [1, 2, 3, 4, 2, 5] # check if numbers is instance of list result = isinstance(numbers, list) print(result) # Output: True
1. isinstance() Syntax
The syntax of isinstance()
is:
isinstance(object, classinfo)
2. isinstance() Parameters
isinstance()
takes two parameters:
- object –
object
to be checked - classinfo – class, type, or tuple of classes and types
3. isinstance Return Value
isinstance()
returns:
True
if the object is an instance or subclass of a class or any element of the tupleFalse
otherwise
If classinfo is not a type or tuple of types, a TypeError
exception is raised.
4. Example 1: How isinstance() works?
class Foo: a = 5 fooInstance = Foo() print(isinstance(fooInstance, Foo)) print(isinstance(fooInstance, (list, tuple))) print(isinstance(fooInstance, (list, tuple, Foo)))
Output
True False True
5. Example 2: Working of isinstance() with Native Types
numbers = [1, 2, 3] result = isinstance(numbers, list) print(numbers,'instance of list?', result) result = isinstance(numbers, dict) print(numbers,'instance of dict?', result) result = isinstance(numbers, (dict, list)) print(numbers,'instance of dict or list?', result) number = 5 result = isinstance(number, list) print(number,'instance of list?', result) result = isinstance(number, int) print(number,'instance of int?', result)
Output
[1, 2, 3] instance of list? True [1, 2, 3] instance of dict? False [1, 2, 3] instance of dict or list? True 5 instance of list? False 5 instance of int? True
Related posts:
Python String split()
Python Program to Find the Factors of a Number
Python Machine Learning Second Edition - Sebastian Raschka & Vahid Mirjalili
Python Program to Check If a List is Empty
Building Machine Learning Systems with Python - Willi Richert & Luis Pedro Coelho
Python Modules
Python Tuple index()
Python Machine Learning Eqution Reference - Sebastian Raschka
Python Program to Split a List Into Evenly Sized Chunks
Python List Comprehension
Python Errors and Built-in Exceptions
Python all()
Python Strings
Python Program to Find Numbers Divisible by Another Number
Python Program to Sort Words in Alphabetic Order
Python Program to Check If Two Strings are Anagram
Python Program to Check if a Key is Already Present in a Dictionary
Python pass statement
Python Set union()
Python Program to Extract Extension From the File Name
Python staticmethod()
Python Program to Transpose a Matrix
Python Set intersection()
Python Program to Get the Last Element of the List
Learning scikit-learn Machine Learning in Python - Raul Garreta & Guillermo Moncecchi
Python Program to Get a Substring of a String
Python Program to Remove Duplicate Element From a List
Python Program to Find All File with .txt Extension Present Inside a Directory
Python __import__()
Python Program to Convert Decimal to Binary Using Recursion
Python frozenset()
Python String rpartition()