“Überprüfen Sie, ob eine Variable Python existiert” 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

Wenn Variable Python existiert

# If the variable exists at all:
if "myVar" in locals() or globals():
	# `myVar` exists


# If the variable is local: 
if "myVar" in locals():
	# `myVar` is local

# If the variable is global:
if "myVar" in globals():
	# `myVar` is global
Temerold

JS überprüfen, ob die Funktion vorliegt

if (typeof yourFunctionName == 'function') { 
  yourFunctionName(); 
}
Dark Dunlin

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 “Überprüfen Sie, ob eine Variable Python existiert”

Fragen ähnlich wie “Überprüfen Sie, ob eine Variable Python existiert”

Weitere verwandte Antworten zu “Überprüfen Sie, ob eine Variable Python existiert” auf TypeScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen