2 function checks whether the object or variable is an instance of the specified class type or data type.
3 checks if
4 is an instance of a class
Also, Solve: Python Basic Exercise and Beginners Quiz
Table of contents
How To Use isinstance() Function in Python
Let’s see the syntax first before moving to the example.
2 function, we can test whether an object/variable is an instance of the specified type or class such as int or list. In the case of inheritance, we can checks if the specified class is the parent class of an object.
Using isintance() we can verify whether a variable is a number or string. Let’s assume variable
2, and you want to check whether num is an instance of an int type.
As we can see in the output, the
5 hold an integer value.
Note: If the
8 argument is not a Class, type, or tuple of types, a
7 exception is raised.
isinstance() With Built-In Types
As you know, Every value (variable) in Python has a type. In Python, we can use different built-in types such as
9, list, tuple, strings, dictionary. Most of the time, you want to check the type of value to do some operations. In this case,
2 function is useful.
Note: If we use the
2 with any variable or object with a
2, it returns
3. Let see the simple example of it.
isinstance() With Multiple Classes
You can also check the instance with multiple types. Let’s say you have a variable, and you wanted to check whether it holds any numeric value or not, for example, a numeric value can be an
To verify whether a variable is an instance of one of the specified types, we need to mention all types in a tuple and pass it to the classInfo argument of
isinstance() With Python Class
2 works as a comparison operator, and it compares the object with the specified class type.
You can verify if the
8 object is an instance of a user-defined class Employee using the
2 function. It must return True.
isinstance() function With Inheritance
The object of the subclass type is also a type of parent class. For example, If Car is a subclass of a Vehicle, then the object of Car can be referred to by either Car or Vehicle. In this case, the
0 will return
2 function works on the principle of the is-a relationship. The concept of an is-a relationship is based on class inheritance.
3 returns True if the classinfo argument of the instance() is the object’s class’s parent class.
To demonstrate this, I have created two classes, Developer and PythonDeveoper. Here PythonDeveoper is a sub-class of a Developer class.
2 function is beneficial for casting objects at runtime because once you get to know the given class is a subclass of a parent class, you can do casting appropriately if required.
isinstance with Python list
As you know, a Python list is used to store multiple values at the same time. These values can be of any data type like numbers, strings, or any Class objects.
In this section, we will test the following operations with the Python list using the
Checking if an object is an instance of a list type
Check if an element of a list is a nested list
To check if one of the elements in the list is itself a list. For example, you have the following list, Use the
2 to verify if the list contains a nested list
Iterate a list and verify each element’s class, and if it a list type, we can say that the list contains a nested list.
Check if elements of a list are numbers or strings
Check each element’s type with multiple numeric types such as
How are variables initialized in Python?
Python has no command for declaring a variable. A variable is created the moment you first assign a value to it.
How do you check variables in Python?
To get the type of a variable in Python, you can use the built-in type() function.
How do you check if a variable is undefined in Python?
Python Code: try: x = 1 except NameError: print("Variable is not defined....!") else: print("Variable is defined.") try: y except NameError: print("Variable is not defined....!") else: print("Variable is defined.") Sample Output: Variable is defined.
How to check if environment variable exists in Python?
# Checking if an Environment Variable Exists in Python import os if 'USER' in os. environ: print('Environment variable exists! ') else: print('Environment variable does not exist. ') # Returns: # Environment variable exists!