Failing Case
I ran across an UnboundLocalError
exception that I could not understand.
I isolated the simplest chunk of the code:
def wrapper_func():
x = 0
def test_unbound():
x += 1
return x
return test_unbound()
Then if I call it:
>>> wrapper_func()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in wrapper_func
File "<stdin>", line 4, in test_unbound
UnboundLocalError: local variable 'x' referenced before assignment
But as I understood it, x
should be available to the inner function.
Functioning Case
I tested it by doing something almost identical:
def outer_func():
x = 1
def inner_func(val):
print(val + x)
inner_func(2)
If I test this out, no error:
outer_func()
3
What Am I Missing?
Can someone help me understand why the failing case fails and the successful case works?