Python is one of the best and efficient high-level programming languages. It has a very straightforward and simple syntax. It has very built-in modules and functions that help us to perform the basic tasks efficiently. The Python isinstance() function evaluates either the given object is an instance of the specified class or not.
This article describes the Python isinstance() function with the assistance of simple examples.
Syntax of isinstance() function
The isinstance() is a Python built-in function. The isinstance() function takes two parameters as an argument i.e. the object and the class type. The syntax of the isinstance() function is as follows:
Both the parameters are required for the isinstance() function. The class type parameter can contain a type of a class or a tuple of classes. The object is checked with the class type. The isinstance() function returns true if the given object is a type or instance of the specified class or tuple of classes; otherwise, it returns false. The Python interpreter throws an error if we specify the wrong class, which is not given as a second argument.
Let’s see the examples of isinstance() function.
Examples
In the given example, we are declaring a string type “name” variable and checking that whether it is an instance of the “str” class or not.
name = "Kamran"
#using the isinstance() function
print("The given variable is the instance of string class: ",isinstance(name,str))
Output
The output is displayed on the Python console. The isinstance() function returns true because the name is the instance of “str” class.
If you change the class type to int instead of str. You will see that the isinstance() function will return false because the name is not the instance of integer class.
name = "Kamran"
#using the isinstance() function
print("The given variable is the instance of integer class: ",isinstance(name,int))
Output
The output is displayed on the Python console. The isinstance() function returns false because the name is not an instance of integer class.
Now let’s declare a number and apply the isinstance() function.
age = 25
#using the isinstance() function
print("The given variable is the instance of integer class: ",isinstance(age,int)
Output
The output is displayed on the Python console.
A tuple of the classes type
The isinstance() function allows us to declare a tuple of classes. In this case, the object is checked against multiple classes. If the object is the instance of any one class from the given classes, then the isinstance() function returns true; otherwise, it returns false.
Let’s declare a tuple of classes type and see what happens.
age = 25
#using the isinstance() function
print("The given variable is the instance of the class: ",isinstance(age,(str,float,list,int,tuple,dict)))
Output
The output is displayed on the Python console. The isinstance() function returns true because the age object is the instance of integer class, and integer class is mentioned inside the tuple of classes type.
If we remove the integer classes from the classes tuple, then the isinstance() function will return false.
age = 25
#using the isinstance() function
print("The given variable is the instance of the class: ",isinstance(age,(str,float,list,tuple,dict)))
Output
The output is displayed on the Python console.
Conclusion
The isinstance() function is a built-in function in Python. It is used to evaluate the type of object against a specified. This article explains the use of the isinstance() function with the help of simple examples.
from Linux Hint https://ift.tt/34BdUkQ
0 Comments