A newbie quesiton: local variable in a nested funciton

Ben Finney ben+python at benfinney.id.au
Fri Dec 25 22:41:25 EST 2015


jfong at ms4.hinet.net writes:

> In the first situation, the local variable 'counter' can be referenced
> correctly. But in the second, why a statement added after the print()
> statement can makes this variable "disappear", even the print() won't
> do the right thing. Isn't it wired? please help!

The Python FAQ answers this, even using an example the same as yours
<URL:https://docs.python.org/3/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value>.

-- 
 \          “… a Microsoft Certified System Engineer is to information |
  `\     technology as a McDonalds Certified Food Specialist is to the |
_o__)                               culinary arts.” —Michael Bacarella |
Ben Finney




More information about the Python-list mailing list