Local scope gone wrong
examples/advanced/scoping_external_variable.py
a = 42 def f(): print(a) a = 23 print('ok') print(a) f() print(a)
ok 42 Traceback (most recent call last): File "scoping_external_variable.py", line 8, in <module> f() File "scoping_external_variable.py", line 3, in f print(a) UnboundLocalError: local variable 'a' referenced before assignment
Accessing a global variable inside a function works, but if I change it (make it refer to another piece of data), then it is disallowed. If I only change the data inside (for mutable variables), that works, but is a bad practice.