Table of Contents
The isinstance() function returns true if the first argument of the function is an instance or subclass of the second argument. Actually, we can say that this function is used to check if the given object is an instance or subclass of the given class.
isinstance(object, classinfo) # Where object specify name of the object
“Ready to take your python skills to the next level? Sign up for a free demo today!”
isinstance() Parameters:
Takes 2 parameters where a first parameter is an object of string, int, float, long, or custom type.
Parameter | Description | Required / Optional |
---|---|---|
object | An object to be checked. | Required |
classinfo | The class name or a tuple of class names. | Required |
isinstance() Return Value
1: Which of the following data types is immutable in Python?
It returns a boolean value either true or false.
Input | Return Value |
---|---|
an object is an instance | true |
an object is not an instance | false |
classinfo is not a type or tuple of types | TypeError exception |
Grab the opportunity to learn Python with Entri! Click Here
Examples of isinstance() method in Python
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
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
Example 3: working of isinstance() another example?
# Python isinstance() function example
class Student:
id = 101
name = "John"
def __init__(self, id, name):
self.id=id
self.name=name
student = Student(1010,"John")
lst = [12,34,5,6,767]
# Calling function
print(isinstance(student, Student)) # isinstance of Student class
print(isinstance(lst, Student))
Output:
True False
Learn to code from industry experts! Enroll here
Python isinstance() method
Python isinstance() function returns True if the object is specified types, and it will not match then return False.
Syntax : isinstance(obj, class)
Parameters :
- obj : The object that need to be checked as a part of class or not.
- class : class/type/tuple of class or type, against which object is needed to be checked.
Returns : True, if object belongs to the given class/type if single class is passed or any of the class/type if tuple of class/type is passed, else returns False. Raises
TypeError: if anything other than mentioned valid class type.
Python isinstance() Example
Example 1: Python isinstance with int and list
# Python 3 code to demonstrate # working of isinstance() # with native types # initializing native types test_int = 5 test_list = [ 1 , 2 , 3 ] # testing with isinstance print ( "Is test_int integer? : " + str ( isinstance (test_int, int ))) print ( "Is test_int string? : " + str ( isinstance (test_int, str ))) print ( "Is test_list integer? : " + str ( isinstance (test_list, int ))) print ( "Is test_list list? : " + str ( isinstance (test_list, list ))) # testing with tuple print ( "Is test_int integer or list or string? : " + str ( isinstance (test_int, ( list , int )))) |
Output:
Is test_int integer? : True Is test_int string? : False Is test_list integer? : False Is test_list list? : True Is test_int integer or list or string? : True
Example 2: Demonstrating the use of isinstance() with objects
# Python 3 code to demonstrate # working of isinstance() # with objects # declaring classes class gfg1: a = 10 # inherited class class gfg2(gfg1): string = 'GeeksforGeeks' # initializing objects obj1 = gfg1() obj2 = gfg2() # checking instances print ( "Is obj1 instance of gfg1? : " + str ( isinstance (obj1, gfg1))) print ( "Is obj2 instance of gfg2? : " + str ( isinstance (obj2, gfg2))) print ( "Is obj1 instance of gfg2? : " + str ( isinstance (obj1, gfg2))) # check inheritance case # return true print ( "Is obj2 instance of gfg1? : " + str ( isinstance (obj2, gfg1))) |
Output:
Is obj1 instance of gfg1? : True Is obj2 instance of gfg2? : True Is obj1 instance of gfg2? : False Is obj2 instance of gfg1? : True
Example 3: Python isinstance array
test_list = [ 1 , 2 , 3 ] print ( "Is test_list list? : " + str ( isinstance (test_list, list ))) |
Output:
Is test_list list? : True
Learn Coding in your Language! Enroll Here!
Example 4: Python isinstance string
test_str = "GeeksforGeeks" print ( "Is test_str string? : " + str ( isinstance (test_str, str ))) |
Output:
Is test_str string? : True
Example 4: Python isinstance dict
test_dict = { "apple" : 1 , "Ball" : 2 } print ( "Is test_str dictionary? : " + str ( isinstance (test_dict, dict ))) |
Output:
Is test_str dictionary? : True
Example 5: Python isinstace class methods
class geeks: course = 'DSA' def purchase(obj): return obj.course geeks.purchase = classmethod (geeks.purchase) str ( isinstance (geeks.purchase(), str )) |
Output:
True