“Como verificar se existe uma variável no Python” Respostas de código

Como verificar se existe uma variável no 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

como verificar se o var existe python

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

Verifique se a variável existe python

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

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

Respostas semelhantes a “Como verificar se existe uma variável no Python”

Perguntas semelhantes a “Como verificar se existe uma variável no Python”

Procure respostas de código populares por idioma

Procurar outros idiomas de código