“Cómo verificar si existe una variable en Python” Código de respuesta

Cómo verificar si existe una variable en Python

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

#if the variable is global:
if 'myVar' in globals():
  # myVar exists.
Worried Wryneck

Cómo verificar si VAR existe Python

# for local
if 'myVar' in locals():
  # myVar exists.
# for globals
if 'myVar' in globals():
  # myVar exists.
Filthy Fish

Compruebe si existe variable Python

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

# Now you're free to use myVar without Python complaining.
Demo Can

Respuestas similares a “Cómo verificar si existe una variable en Python”

Preguntas similares a “Cómo verificar si existe una variable en Python”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código