The Python’s 2 function checks whether the object or variable is an instance of the specified class type or data type. Show
For example, 3 checks if 4 is an instance of a class 5.Also, Solve: Python Basic Exercise and Beginners Quiz Table of contentsHow To Use isinstance() Function in PythonLet’s see the syntax first before moving to the example. Syntax:
Using 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.
ExampleUsing 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.
Output: Yes As we can see in the output, the 2 returned 4 because 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 TypesAs you know, Every value (variable) in Python has a type. In Python, we can use different built-in types such as Yes5, 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 ClassesYou 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 Yes5 or 9.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 2.Example
isinstance() With Python ClassThe 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 InheritanceThe 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 4.The 2 function works on the principle of the is-a relationship. The concept of an is-a relationship is based on class inheritance.The 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.
Note: The 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 listAs 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 2 function:
Checking if an object is an instance of a list type
Check if an element of a list is a nested listTo 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. 0Check if elements of a list are numbers or stringsCheck each element’s type with multiple numeric types such as Yes5, 9, and 0using the 2 function.
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!
|