Could someone explain why the following program fails:
def g(f):
for _ in range(10):
f()
def main():
x = 10
def f():
print x
x = x + 1
g(f)
if __name__ == '__main__':
main()
with the message:
Traceback (most recent call last):
File "a.py", line 13, in <module>
main()
File "a.py", line 10, in main
g(f)
File "a.py", line 3, in g
f()
File "a.py", line 8, in f
print x
UnboundLocalError: local variable 'x' referenced before assignment
But if I simply change the variable x
to an array, it works:
def g(f):
for _ in range(10):
f()
def main():
x = [10]
def f():
print x[0]
x[0] = x[0] + 1
g(f)
if __name__ == '__main__':
main()
with the output
10
11
12
13
14
15
16
17
18
19
The reason I am confused is, if from f()
it can't access x
, why it becomes accessible if x
is an array?
Thanks.
The reason is in first example you used an assignment operation,
x = x + 1
, so when the functions was defined python thought thatx
is local variable. But when you actually called the function python failed to find any value for thex
on the RHS locally, So raised an Error.In your second example instead of assignment you simply changed a mutable object, so python will never raise any objection and will fetch
x[0]
's value from the enclosing scope(actually it looks for it firstly in the enclosing scope, then global scope and finally in the builtins, but stops as soon as it was found).In python 3x you can handle this using the nonlocal keyword and in py2x you can either pass the value to the inner function or use a function attribute.
Using function attribute:
Passing the value explicitly:
Using
nonlocal
in py3x:The problem is that the variable
x
is picked up by closure. When you try to assign to a variable that is picked up from the closure, python will complain unless you use theglobal
ornonlocal
1 keywords. In the case where you are using alist
, you're not assigning to the name -- You can modify an object which got picked up in the closure, but you can't assign to it.Basically, the error occurs at the
print x
line because when python parses the function, It sees thatx
is assigned to so it assumesx
must be a local variable. When you get to the lineprint x
, python tries to look up a localx
but it isn't there. This can be seen by usingdis.dis
to inspect the bytecode. Here, python uses theLOAD_FAST
instruction which is used for local variables rather than theLOAD_GLOBAL
instruction which is used for non-local variables.Normally, this would cause a
NameError
, but python tries to be a little more helpful by looking forx
infunc_closure
orfunc_globals
2. If it findsx
in one of those, it raises anUnboundLocalError
instead to give you a better idea about what is happening -- You have a local variable which couldn't be found (isn't "bound").1python3.x only
2python2.x -- On python3.x, those attributes have changed to
__closure__
and__globals__
respectivelyThe problem is in the line
This is the first time
x
being assigned in functionf()
, telling the compiler thatx
is a local name. It conflicts with the previous lineprint x
, which can't find any previous assignment of the localx
. That's where your errorUnboundLocalError: local variable 'x' referenced before assignment
comes from.Note that the error happens when compiler tries to figure out which object the
x
inprint x
refers to. So theprint x
doesn't executes.Change it to
No new name is added. So the compiler knows you are referring to the array outside
f()
.You have to understand the order in which things happen. Before your python code is even compiled and executed, something called a parser reads through the python code and checks the syntax. Another thing the parser does is mark variables as being local. When the parser sees an assignment in the code in a local scope, the variable on the lefthand side of the assignment is marked as local. At that point, nothing has even been compiled yet--let alone executed, and therefore no assignment takes place; the variable is merely marked as a local variable.
After the parser is finished, the code is compiled and executed. When execution reaches the print statement:
the print statement looks like it should go ahead and print the x in the enclosing scope (the 'E' in the LEGB lookup process). However, because the parser previously marked x as a local variable inside f(), python does not proceed past the local scope (the 'L' in the LEGB lookup process) to lookup x. Because x has not been assigned to in the local scope at the time 'print x' executes, python spits out an error.
Note that even if the code where an assignment occurs will NEVER execute, the parser still marks the variable on the left of an assignment as a local variable. The parser has no idea about how things will execute, so it blindly searches for syntax errors and local variables throughout your file--even in code that can never execute. Here are some examples of that:
The parser does the same thing when marking local variables:
Now look what happens when you execute that last program:
When the statement 'print x' executes, because the parser marked x as a local variable the lookup for x stops at the local scope.
That 'feature' is not unique to python--it happens in other languages too.
As for the array example, when you write:
that tells python to go lookup up an array named x and assign something to its first element. Because there is no assignment to anything named x in the local scope, the parser does not mark x as a local variable.