Newbie question
Ante
abagaric at rest-art.hr
Tue Apr 16 11:49:19 EDT 2002
from file 'dummy.py'
----------------------------
num = 0
def dummy():
num += 1
dummy()
----------------------------
now, in the interpreter:
>>> import dummy
Traceback (most recent call last):
File "<string>", line 1, in ?
File "C:\downloads\python2.2.1\dummy.py", line 6, in ?
dummy()
File "C:\downloads\python2.2.1\dummy.py", line 4, in dummy
num += 1
UnboundLocalError: local variable 'num' referenced before assignment
Why does this happen?
If I replace num += 1 with print num, it prints 0 without an error.
I guess it scans the whole function and check if there's an assignment to a
name,
and if there is, assumes it's a local variable. But += isn't supposed to
move the
reference but instead change the same variable. Or what did I get wrong?
Ante.
More information about the Python-list
mailing list