“So überprüfen Sie, ob in Python eine Variable vorhanden ist” Code-Antworten

So überprüfen Sie, ob in Python eine Variable vorhanden ist

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

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

So überprüfen Sie, ob var Python existiert

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

Überprüfen Sie, ob eine Variable Python existiert

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

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

Ähnliche Antworten wie “So überprüfen Sie, ob in Python eine Variable vorhanden ist”

Fragen ähnlich wie “So überprüfen Sie, ob in Python eine Variable vorhanden ist”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen