Python

Python isinstance() Function

Python isinstance() Function

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:

isinstance(object, class_type)

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.

#declaring a string variable
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.

#declaring a string variable
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.

#declaring an age variable
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.

#delcaring an age variable
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.

#declaring an age variable
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.

OpenTTD contre Simutrans
Créer votre propre simulation de transport peut être amusant, relaxant et extrêmement attrayant. C'est pourquoi vous devez vous assurer que vous essay...
Tutoriel OpenTTD
OpenTTD est l'un des jeux de simulation d'entreprise les plus populaires. Dans ce jeu, vous devez créer une merveilleuse entreprise de transport. Cepe...
SuperTuxKart pour Linux
SuperTuxKart est un excellent titre conçu pour vous apporter l'expérience Mario Kart gratuitement sur votre système Linux. C'est assez stimulant et am...