As a tranditional language programmer like me, the result is really weird.
Here is the test codes in file test1.py:
--------
def outerf():
counter = 55
def innerf():
print(counter)
#counter += 1
return innerf
myf = outerf()
--------
the result is:
--------
>>> import test1
>>> test1.myf()
55
>>>
--------
that's OK. But if I un-comment the line "counter += 1", then it gives me this:
--------
>>> import test1
>>> test1.myf()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Work\Python34\test1.py", line 41, in innerf
print(counter)
UnboundLocalError: local variable 'counter' referenced before assignment
>>>
--------
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!
--
https://mail.python.org/mailman/listinfo/python-list