33

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
crk
  • 617
  • 1
  • 8
  • 12

5 Answers5

67

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
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306
  • 5
    What a good explanation! This Python's dark corner of identifying scope by mere **existence** of assignment **anywhere** in a function (much towards the end in my case) just drove me mad when I desperately searched for explanation in other SO answers as well as outer internet. And drove even more mad when I was encountering some counter-examples **not** producing damn error, with explanations that accessing globals is all ok in Python! Finally, got enlightened here! ^_^ – Van Jone Feb 21 '13 at 17:11
6

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.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
3

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

No, because c += 3 is the same as:

c = c + 3
    ^
    |
and funcB does not know what this c is

2) I don't have a choice of making c a global variable and then declaring global c in funcB.

Please don't do that, just change:

def funcB():

with:

def funcB(c):

and call funcB(c) later in your code.

Note: You should also cosider to define funcB and funcC outside funcA

Rik Poggi
  • 28,332
  • 6
  • 65
  • 82
1

Another dirty workaround, which, however, doesn't require you to make c global. Everything the same, but:

def funcB():
    globals()['c'] += 3
    print "funcB", c
dmytro
  • 1,293
  • 9
  • 21
  • 1
    well, the whole thing in the way it is asked is not very elegant, but just to make it work - this approach works. – dmytro Jan 19 '12 at 23:49
0

Try this:

def funcA():
   print "funcA"
   c = 0
   def funcB(c):
      c += 3
      print "funcB", c

   def funcC(c):
      c = 5
      print "funcC", c

   print "c", c
   funcB(c)
   funcC(c)
   funcB(c)
   funcC(c)
   print "end"

funcA()

And if you want to remember c value then:

def funcA():
   print "funcA"
   c = 0
   def funcB(c):
      c += 3
      print "funcB", c
      return c

   def funcC(c):
      c = 5
      print "funcC", c
      return c

   print "c", c
   c = funcB(c)
   c = funcC(c)
   c = funcB(c)
   c = funcC(c)
   print "end"

funcA()

that will produce:

funcA
c 0
funcB 3
funcC 5
funcB 8
funcC 5
end

C:\Python26\
joaquin
  • 82,968
  • 29
  • 138
  • 152