Determine if variable is defined in Python
I think it's better to avoid the situation. It's cleaner and clearer to write:
a = None
if condition:
a = 42
try:
thevariable
except NameError:
print("well, it WASN'T defined after all!")
else:
print("sure, it was defined.")
'a' in vars() or 'a' in globals()
if you want to be pedantic, you can check the builtins too'a' in vars(__builtins__)