In different programming languages, the type assigned to the variable can not be changed from what is declared at the initial stage. Whereas in Python, the case is different. A value of integer type can be declared and the string value can be declared to the same variable. In Python, a variable is known as the “pointer to the object” or the reference to an object. At times, there arises a need to check for the variable existence in Python.
In this article, we will depict different methods to check whether the variable exists or not.
How to Check for the Existence of Variable in Python?
The existence of variables in a code is an important aspect of programming in Python. Since variables play a vital role in the declaration of the object. Therefore, in Python, there are two methods to declare a variable while coding. The first way is to declare it “locally” and the second way is to declare it “globally”. In order to check for the existence of variables in Python there are three different ways listed below:
- Using Local variable
- Using global variable
- Using the hasattr() method of Python
Method 1: Using Local Variable
The variable that is declared within the function in Python is referred to as a “local” variable. The below code checks if the variable is present in locals().
#Declare the function
def Method1():
#Declare the variable
variable1 = "Python Programming"
#The if statement to check for the string
if 'variable1' in locals():
#Print statement
print ('The declared variable is found')
else:
print ('The declared variable is not found')
#Calling the function
Method1()
In the above code:
- The Function is declared as “Method1”.
- The next step involves declaring a variable as “variable1”.
- The if loop in the next step checks for the “variable1” using the locals() function.
- The function is called at the end of the code.
Output
The output below shows that variable 1 is present.
Method 2: Using Global Variable
If the variable is declared outside the main function, it is described as a “global” variable. The globals() function checks for the existence of the variable.
var1 = "Coding"
def Method2():
if 'var1' in globals():
print ("The declared variable is present in global")
else:
print ("The declared variable is not present in global")
Method2()
In the above code block:
- The var1 is declared outside the function.
- The globals() function checks if the variable is either declared inside the function or outside the function.
- The function Method2() is called at the end.
Output
The output below shows that the variable var1 is declared outside the function therefore it is global.
Method 3: Using hasattr() Function
The hasattr() function takes two parameters: the object and the name. The variable is checked for its existence using the hasattr() function of Python.
#Declare the class
class Programminglanguages:
#Declare the variables
Name = 'Python'
Level = 'Intermediate'
pl = Programminglanguages()
#The hasattr function checks for the existence of variables.
if hasattr(pl, 'Name'):
print("The name of Language is :", pl.Name)
if hasattr(pl, 'Level'):
print("Programming Level:", pl.Level)
In the above code:
- The class is declared as “Programminglanguages”.
- The variables “Name” and “Level” are declared.
- The hasattr() function checks for the variable presence in the code.
- The print statement prints the output accordingly.
Output
The output below shows that the Python and Intermediate are printed.
This ends the discussion to check for the existence of a variable in Python
Conclusion
Variables in Python are the reference to the objects and their presence matters while coding. The variable presence in Python can be checked using globals(), locals(), and hasattr() function. In this article, we have effectively demonstrated the implementation of different methods to check for the existence of variables in Python.