I want to check if a variable exists. Now I'm doing something like this:
try:
myVar
except NameError:
# Do something.
Are there other ways without exceptions?
exceptionpythonvariables
I want to check if a variable exists. Now I'm doing something like this:
try:
myVar
except NameError:
# Do something.
Are there other ways without exceptions?
Best Answer
To check the existence of a local variable:
To check the existence of a global variable:
To check if an object has an attribute: