Answers for "check if value exists in variable python"

11

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
-1

check if variable exists python

try:
    myVar
except NameError:
    myVar = None      # or some other default value.

# Now you're free to use myVar without Python complaining.
Posted by: Guest on April-22-2022

Code answers related to "check if value exists in variable python"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language