Functions: Global and Local Scope (Livehacking Screenplay)¶
Undefined Variable¶
>>> def f():
... print(x)
...
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in f
NameError: name 'x' is not defined
Local Variable¶
>>> def f():
... x = 1
... print(x)
...
>>> f()
1
- Discuss:
x
ist not auomatically in global scope (like e.g. Javascript)
Global Variable¶
>>> def g():
... print(globvar)
...
>>> globvar = 42
>>> g()
42
Local and Global Variable With Same Name¶
x = 666
def f():
x = 1
print('local:', x)
f()
print('global:', x)
$ python code/30-local-global-same-name.py
local: 1
global: 666
Assignment to Global Variable¶
How do I assign to a global variable then, if assignment creates a
variable in local scope? ⟶ global
keyword
x = 666
def f():
global x
x = 1
print('assigned global:', x)
f()
print('global:', x)
$ python code/40-global-assignment.py
assigned global: 1
global: 1