local variable 'a' referenced b

contro opinion contropinion at gmail.com
Tue Oct 2 22:03:20 EDT 2012


code1
>>> def foo():
...     a = 1
...     def bar():
...         b=2
...         print a + b
...     bar()
...
...
>>> foo()
3

code2
>>> def foo():
...     a = 1
...     def bar():
...         b=2
...         a = a + b
...         print a
...     bar()
...
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in foo
  File "<stdin>", line 5, in bar
UnboundLocalError: local variable 'a' referenced b

why code2 can not get output of 3?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121002/86652bc6/attachment.html>


More information about the Python-list mailing list