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.


nonlocalkeyword...