Python isinstance()

Posted August 6, 2022 by  ‐ 1 min read

In python isinstance() method is used to check the datatype.

Syntax

ininstance(obj: 'Any Object instance', type: 'defined type') -> bool

Example 1:

my_list = [1, 2]
is_list = isinstance(my_list, list)
# is_list value: True

Example 2:

# define a class
class MyClass:
    pass

# create an instance of class
my_obj = MyClass()
is_myclass_obj = isinstance(my_obj, MyClass)
# is_myclass_obj value: True

is_myclass_obj = isinstance(my_obj, list)
# # is_myclass_obj value: False

Subscribe For More Content