Python isinstance() Function
The isinstance()
function is a function used for type checking: it verifies if an object is of a specified type, returning True
if it is, and False
otherwise
Syntax
isinstance(object, classinfo)
isinstance() Parameters
Python isinstance()
function parameters:
Parameter | Condition | Description |
---|---|---|
object | Required | The object that needs to be checked. |
classinfo | Required | The class, type, or a tuple of classes/types against which the object is checked. |
isinstance() Return Value
Python isinstance()
function 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.
Examples
Example 1: basic example with isinstance() function
class MyClass:
a = 123
myClassInstance = MyClass()
print(isinstance(myClassInstance, MyClass)) # Output: True
print(isinstance(myClassInstance, (list, tuple))) # Output: False
print(isinstance(myClassInstance, (list, tuple, MyClass))) # Output: True
output
True
False
True
Notice that:
isinstance(myClassInstance, MyClass)
checks ifmyClassInstance
is an instance of theMyClass
class. SincemyClassInstance
is created from theMyClass
class, this will returnTrue
.isinstance(myClassInstance, (list, tuple))
checks ifmyClassInstance
is an instance of either thelist
ortuple
classes. SincemyClassInstance
is not an instance of eitherlist
ortuple
, this will returnFalse
.isinstance(myClassInstance, (list, tuple, MyClass))
checks ifmyClassInstance
is an instance of any of the classes in the tuple(list, tuple, MyClass)
. SincemyClassInstance
is an instance ofMyClass
, this will returnTrue
.
The third isinstance()
line demonstrates that isinstance()
function can check against multiple types by passing a tuple of types as the second argument
Example 2: Checking for Native Types with isinstance() function
Checking for Native Types Using Python isinstance()
function:
a_list = [1, 2, 3, 4]
print(isinstance(a_list, list)) # Output: True
print(isinstance(a_list, tuple)) # Output: False
print(isinstance(a_list, set)) # Output: False
output
True
False
False
Example 3: Checking for Multiple Types with isinstance() function
Check against multiple types using Python isinstance()
function:
a_list = [1, 2, 3, 4]
print(isinstance(a_list, (tuple, list))) # Output: True
print(isinstance(a_list, (tuple, set))) # Output: False
print(isinstance(a_list, (tuple, list, set))) # Output: True
output
True
False
True
Example 4: Checking for Custom Class Types with isinstance() function
Check if a certain instance is of a user-defined class using the Python isinstance() function:
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f'Hi! My name is {self.name}')
Nik = Person('Tom')
print(isinstance(Nik, Person)) # Output: True
print(isinstance(Nik, tuple)) # Output: False
print(isinstance(Nik, set)) # Output: False
output
True
False
False
Example 5: Checking for Inherited Class Types with isinstance() function
Checking if an instance is a subclass:
class Developer:
pass
class PythonDeveloper(Developer):
pass
class JavaDeveloper(Developer):
pass
dev = Developer()
devPython = PythonDeveloper()
devJava = JavaDeveloper()
print(isinstance(devPython, Developer)) # Output: True
print(isinstance(devJava, Developer)) # Output: True
print(isinstance(dev, PythonDeveloper)) # Output: False
print(isinstance(dev, JavaDeveloper)) # Output: False
print(isinstance(devPython, PythonDeveloper)) # Output: True
print(isinstance(devPython, JavaDeveloper)) # Output: False
print(isinstance(devJava, PythonDeveloper)) # Output: False
print(isinstance(devJava, JavaDeveloper)) # Output: True
output
True
True
False
False
True
False
False
True
Example 6: Implement a Basic Type Checking Function using isinstance() function
Let's create a function that returns a string message according to the type of the object given to our check_type
function:
def check_type(obj):
if isinstance(obj, int):
return "It's an integer!"
elif isinstance(obj, str):
return "It's a string!"
else:
return "I don't know what this is."
print(check_type(42)) # Output: It's an integer!
print(check_type("Hello")) # Output: It's a string!
print(check_type(3.14)) # Output: I don't know what this is.
output
It's an integer!
It's a string!
I don't know what this is.