Globales Python - Schlüsselwort
Beispiel
Deklarieren Sie eine globale Variable innerhalb einer Funktion und verwenden Sie sie außerhalb der Funktion:
#create a function:
def myfunction():
global x
x =
"hello"
#execute the function:
myfunction()
#x should now
be global, and accessible in the global scope.
print(x)
Definition und Verwendung
Das global
Schlüsselwort wird verwendet, um globale Variablen aus einem nicht-globalen Gültigkeitsbereich zu erstellen, zB innerhalb einer Funktion.