Answers for "python check if variable exists then use"

10

how to check if a variable exists in python

#if the variable is local: 
if 'myVar' in locals():
  # myVar exists

#if the variable is global:
if 'myVar' in globals():
  # myVar exists.
Posted by: Guest on October-03-2020
0

check if variable is defined in python

try: x
except NameError: some_fallback_operation(  )
else: some_operation(x)
Posted by: Guest on August-07-2021

Code answers related to "python check if variable exists then use"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language