27 C
Jaipur
Saturday, December 5, 2020

Python isinstance() Function – Linux Hint

Must read

Flipkart Announces ‘2GUD Local’ to Connect Offline Retailers with Online Buyers

Flipkart‘s discount retail platform, 2GUD, has introduced a new service to extend the benefits of e-commerce to traditional offline retailers. Called ‘2GUD Local’, the...

40 Apple iPhones, iPads, MacBooks that are officially outdated

Apple has a ‘list’ of products that it categorises as “vintage” and “obsolete”. As per Apple, “vintage” products are those that have not been...

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.

isinstance output 2

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.

tupleclasses output

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.

tupleclasses output2

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Flipkart Announces ‘2GUD Local’ to Connect Offline Retailers with Online Buyers

Flipkart‘s discount retail platform, 2GUD, has introduced a new service to extend the benefits of e-commerce to traditional offline retailers. Called ‘2GUD Local’, the...

40 Apple iPhones, iPads, MacBooks that are officially outdated

Apple has a ‘list’ of products that it categorises as “vintage” and “obsolete”. As per Apple, “vintage” products are those that have not been...

This miniature gaming laptop could power three 4K monitors as a workstation PC

What is it? The GPD Win Max is a powerful Windows 10 2-in-1 convertible laptop that doubles as a gaming notebook.What makes it special?...