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.

Port Sumber Terbuka dari Mesin Game Komersial
Rekreasi mesin game gratis, open source, dan lintas platform dapat digunakan untuk bermain lama serta beberapa judul game yang cukup baru. Artikel ini...
Game Baris Perintah Terbaik untuk Linux
Baris perintah bukan hanya sekutu terbesar Anda saat menggunakan Linux-ini juga dapat menjadi sumber hiburan karena Anda dapat menggunakannya untuk me...
Aplikasi Pemetaan Gamepad Terbaik untuk Linux
Jika Anda suka bermain game di Linux dengan gamepad alih-alih sistem input keyboard dan mouse biasa, ada beberapa aplikasi yang berguna untuk Anda. Ba...