For the following Python 2.7 code:
#!/usr/bin/python
def func_a():
print "func_a"
c = 0
def func_b():
c += 3
print "func_b", c
def func_c():
print "func_c", c
print "c", c
func_b()
c += 2
func_c()
c += 2
func_b()
c += 2
func_c()
print "end"
func_a()
I get the following error:
File "./a.py", line 9, in func_b
c += 3
UnboundLocalError: local variable 'c' referenced before assignment
But when I comment out the line c += 3 in func_b, I get the following output:
func_a
c 0
func_b 0
func_c 2
func_b 4
func_c 6
end
Isn’t c being accessed in both cases of += in func_b and = in func_c? Why doesn’t it throw error for one but not for the other?
I don’t have a choice of making c a global variable and then declaring global c in func_b. Anyway, the point is not to get c incremented in func_b but why it’s throwing error for func_b and not for func_c while both are accessing a variable that’s either local or global.
What you are seeing here is the difference between accessing and assigning variables. In Python 2.x you can only assign to variables in the innermost scope or the global scope (the latter is done by using the global statement). You can access variables in any enclosing scope, but you cannot access a variable in an enclosing scope and then assign to it in the innermost or global scope.
What this means is that if there is any assignment to a name inside of a function, that name must already be defined in the innermost scope before the name is accessed (unless the global statement was used). In your code the line
c += 3is essentially equivalent to the following:Because there is an assignment to
cin the function, every other occurrence ofcin that function will only look in the local scope forfuncB. This is why you see the error, you are attempting to accesscto get its current value for the+=, but in the local scopechas not been defined yet.In Python 3 you could get around this issue by using the nonlocal statement, which allows you to assign to variables that are not in the current scope, but are also not in the global scope.
Your code would look something like this, with a similar line at the top of
funcC:In Python 2.x this isn’t an option, and the only way you can change the value of a nonlocal variable is if it is mutable.
The simplest way to do this is to wrap your value in a list, and then modify and access the first element of that list in every place where you had previously just used the variable name:
…and the output: