How do you check if a variable exists?
To check if a local variable exists, use in locals() to check the local scope or in globals() to check the global scope.
The difference between the local and the global scope is:
Variables defined in functions can only be accessed within the function. They are called local variables.
Variables defined outside of functions can be accessed everywhere. They are called global variables.
To check for local variables, use the locals() function.
To check for global variables, use the globals() function.
To check if the variable is defined in local and global scope, use and operator.
To check if the variable is defined in local or global scope, use or operator.
Why is it important to check if variable exists?
It can be helpful to check if a variable actually exists. In Python, variables have to be defined, before they can be called. If a variable is called, that is not defined a NameError exception is raised.
NameError: name 'myVariable' is not defined
To prevent NameError exceptions it is useful to check if a variable actually exists beforehand.
If a variable doesn't exist, call some alternative backup operation instead of using the undefined variable.
# check if variable exists and run alternative operation
if 'myVariable' in globals():
myOriginalFunction('myVariable')
else:
myAlternativeFunction(x)
In this example, it is checked if myVariable exists on a global scope.
If the variable exists myOriginalFunction() is called.
If it does not exists myAlternativeFunction() is called.
check if variable exists in local scope
To check if a variable exists in the local scope, use the locals() function.
# check if variable exists in local scope
if 'myVariable' in locals():
print('variable exists')
check if global variable exists
To check if a global variable exists: use if 'myVariable' in globals().
# check if variable exists in global scope
if 'myVariable' in globals():
print('variable exists')
check if global or local variable exists
To check if the variable exists either in the global or local scope, use the or operator.
To check if the variable exists both in the global and local scope, use the and operator.
# check if variable exists in global or local scope
# first check
if 'myVariable' in globals() or 'myVariable' in locals():
print('myVariable exists')
else:
print('myVariable does not exist')
# declare myVariable on global scope
myVariable = 1
# second check
if 'myVariable' in globals() or 'myVariable' in locals():
print('myVariable exists')
else:
print('myVariable does not exist')
The two checks are there to illustrate the different results.
The first check is done before the variable is defined, it prints 'myVariable doesn't exist'.
The second check is done after the variable is defined, it prints 'myVariable exists'.
myVariable does not exist
myVariable exists
To check if a variable exists both in the global and local scope, use the "and" operator in the same way.
check if object has attribute
Finally to check if an object has an attribute of a specific name, use the hasattr() function.
# check if object has attribute
if hasattr(obj, 'myAttributeName'):
print('attribute exists')