Variablen, die nicht als global innerhalb einer Funktion gekennzeichnet sind, erhalten automatisch einen lokalen Geltungsbereich. Außerhalb der Funktion kann nicht auf sie zugegriffen werden.
def f():
s = "cat"
print(s)
f()
print(s)
Ausgabe des Skriptes:
cat
Traceback (most recent call last):
File "global_local3.py", line 6, in <module>
print s
NameError: name 's' is not defined
>>> global y
>>> y = 9
>>> def f():
... y = 42
...
>>> y
9
>>> f()
>>> y
9
>>> def f():
... global x
... x= 42
...
>>> f()
>>> print(x)
42