The isinstance Python is an in-built function that is based on the concept of objects. Python is an object-oriented language in which classes can be created. The isinstance function can be used in various ways in a Python program. The most important and common use of the function is to know which object belongs to which class in the middle of the program.
The isinstance () function checks whether an object is an instance of the class mentioned. One can test if an object or variable is an instance of the type or class. If inheritance is present in the program, then the function can be used to check if a specified class is the parent class of an object.
The following syntax is followed to implement the isinstance () function in Python.
isinstance (object, classinfo)
isinstance () Parameters
The isinstance Python function requires two parameters to be passed. The first is the object that needs to be checked whether it is a subclass or instance of the specified class or not.
The second argument that needs to be passed in the isinstance () function is the class, type, or tuple of a class or type. The object needs to be checked as an element of this class.
Return Value from isinstance ()
The isinstance () function returns values in the form of true or false. If it returns true, then the object passed belongs to the specified class or type if any single class is passed. If a tuple of classes of types is passed, then the function will return true when the object is an instance for any one of the classes present in the tuple.
Otherwise, the function will return false. If any invalid class type is passed as an argument, then the function will return the Type Error.
Example 1: How isinstance () works?
The program below shows an instance can be created in a class and then it is tested with the help of the isinstance function.
class Test1:
a = 6
testInstance = Test1 ()
print (isinstance (testInstance, Test1))
print (isinstance (testInstance, (list, tuple)))
print (isinstance (testInstance, (list, tuple, Test1)))
Here, the first isinstance function does not use class info parameter. The output for the program will be as follows.
True
False
True
Example 2: Working of isinstance () with Native Types
int_1 = 7
str_1 = “Learn Python”
list_3 = [2, 4, 6]
print (“Is int_1 an integer?” + str (isinstance (int_1, int)))
print (“Is int_1 a string?” + str (isinstance (int_1, str)))
print (“Is str_1 a string?” + str (isinstance (str_1, str)))
print (“Is list_1 an integer?” + str (isinstance (list_1, int)))
print (“Is list_1 a list?” + str (isinstance (list_1, list)))
print (“Is int_1 integer or list or string?” + str (isinstance (int_1, (list, str, int))))
print (“Is list_1 string or tuple?” + str (isinstance (list_1, (str, tuple))))
The output of the program above will be as follows.
Is int_1 an integer? True Is int_1 a string? False Is str_1 a string? True Is list_1 integer? False Is list_1 a list? True Is int_1 integer or list or string? True Is list_1 a string or tuple? False
Here, all the functions of isinstance include the classinfo parameter. When the instance is found to not belong with the class specified in the class info, then the function returns false. Otherwise, it returns the value as true.
Questions
What does isinstance mean in Python?
The isinstance () function is an in-built function in Python that tests whether an object is an instance or subclass of the class mentioned as the class info in the parameters of the function.
How do I use isinstance?
To use the isinstance function, one needs to mention two parameters. The first is the object, which is the object that needs to be checked as the subclass or instance of the class, type, or tuple of classes and types in which the existence of the instance needs to be checked.
What is assert isinstance in Python?
The assert isinstance () in Python is the function that is used in unit testing to check if an instance is an object of a specified class or not.
What is an instance in Python with an example?
An instance is an individual object in Python that belongs to a certain class.
Leave a Reply