Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning to variable from parent function: "Local variable referenced before assignment" [duplicate]

Tags:

python

For the following Python 2.7 code:

#!/usr/bin/python  def funcA():    print "funcA"    c = 0     def funcB():       c += 3       print "funcB", c     def funcC():       print "funcC", c     print "c", c    funcB()    c += 2    funcC()    c += 2    funcB()    c += 2    funcC()    print "end"  funcA() 

I get the following error:

File "./a.py", line 9, in funcB     c += 3 UnboundLocalError: local variable 'c' referenced before assignment 

But when I comment out the line c += 3 in funcB, I get the following output:

funcA c 0 funcB 0 funcC 2 funcB 4 funcC 6 end 

Isn't c being accessed in both cases of += in funcB and = in funcC? 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 funcB. Anyway, the point is not to get c incremented in funcB but why it's throwing error for funcB and not for funcC while both are accessing a variable that's either local or global.

like image 365
crk Avatar asked Jan 19 '12 23:01

crk


People also ask

How do I fix local variable I referenced before assignment?

The Python "UnboundLocalError: Local variable referenced before assignment" occurs when we reference a local variable before assigning a value to it in a function. To solve the error, mark the variable as global in the function definition, e.g. global my_var .

What does local variable count referenced before assignment mean?

The local variable referenced before assignment occurs when some variable is referenced before assignment within a function's body. The error usually occurs when the code is trying to access the global variable.

How do you pass a global variable in python?

If you want to simply access a global variable you just use its name. However to change its value you need to use the global keyword. E.g. This would change the value of the global variable to 55.

What is an unbound local error in Python?

The UnboundLocalError: local variable referenced before assignment error is raised when you try to assign a value to a local variable before it has been declared. You can solve this error by ensuring that a local variable is declared before you assign it a value.


2 Answers

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 += 3 is essentially equivalent to the following:

tmp = c c = tmp + 3 

Because there is an assignment to c in the function, every other occurrence of c in that function will only look in the local scope for funcB. This is why you see the error, you are attempting to access c to get its current value for the +=, but in the local scope c has 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:

   def funcB():       nonlocal c       c += 3       ... 

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:

def funcA():    print "funcA"    c = [0]    def funcB():       c[0] += 3       print "funcB", c[0]     def funcC():       c[0] = 5       print "funcC", c[0]     print "c", c[0]    funcB()    funcC()    funcB()    funcC()    print "end"  funcA() 

...and the output:

funcA c 0 funcB 3 funcC 5 funcB 8 funcC 5 end 
like image 113
Andrew Clark Avatar answered Oct 19 '22 05:10

Andrew Clark


Isn't 'c' being accessed in both cases of '+=' in funcB and '=' in funcC?

No, funcC makes a new variable, also called c. = is different in this respect from +=.

To get the behavior you (probably) want, wrap the variable up in a single-element list:

def outer():     c = [0]     def inner():         c[0] = 3     inner()     print c[0] 

will print 3.

Edit: You'll want to pass c as an argument. Python 2 has no other way, AFAIK, to get the desired behavior. Python 3 introduces the nonlocal keyword for these cases.

like image 45
Fred Foo Avatar answered Oct 19 '22 03:10

Fred Foo