Variables in Python can be defined locally or globally. There are two types of the variable first one is a local variable that is defined inside the function and the second one are global variable that is defined outside the function.
To check the existence of the variable locally we are going to use the locals() function to get the dictionary of the current local symbol table.
Example:
Examples: Checking local variable existence
def func():
# defining local variable
a_variable = 0
# using locals() function
# for checking existence in symbol table
is_local_var = "a_variable" in locals()
# printing result
print(is_local_var)
# driver code
func() |
Output:
True