Maybe my coffee is not strong enough this morning, but this behavior is confusing me right now:
>>> a = 'foo'
>>> def func1():
... print a
...
>>> def func2():
... print a
... a = 'bar'
...
>>> func1()
foo
>>> func2()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in func2
UnboundLocalError: local variable 'a' referenced before assignment
(Note that it's the print a
statement that's raising the error in func2()
, not a = 'bar'
.)
Can somebody explain to me what's going on here?