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.

Top 10 spellen om te spelen op Ubuntu
Het Windows-platform is een van de dominante platforms voor gaming geweest vanwege het enorme percentage games dat tegenwoordig wordt ontwikkeld om Wi...
5 beste arcade-spellen voor Linux
Tegenwoordig zijn computers serieuze machines die worden gebruikt om te gamen. Als je de nieuwe hoge score niet kunt halen, weet je wat ik bedoel. In ...
Strijd om Wesnoth 1.13.6 Ontwikkeling vrijgegeven
Strijd om Wesnoth 1.13.6 die vorige maand werd uitgebracht, is de zesde ontwikkelingsrelease in de 1.13.x-serie en het levert een aantal verbeteringen...