[Tutor] Why thus?

Gerrit Holl gerrit@nl.linux.org
Sun, 12 Mar 2000 20:00:48 +0100


<quote name="Arthur Siegel" date="952860453" email="ajs@ix.netcom.com">
> Can't quite see what's happening here.
> 
> counter=0
> class Count:
>  def __init__(self):
>     print counter
> Count()
> 
> gets :
> 
> 0
> Process completed successfully
> 
> But:
> 
> counter=0
> class Count:
>  def __init__(self):
>   counter=counter+1
>   print counter
> Count()
> 
> gets:
> 
> Traceback (innermost last):
>   File "C:\temp\test.py", line 6, in ?
>     Count()
>   File "C:\temp\test.py", line 4, in __init__
>     counter=counter+1
> NameError: counter
> 
> Process completed with exit code 1
</quote>

Use the "global" statement, i.e. "global counter" within
__init__.

regards,
Gerrit.

-- 
1011001 1101111 1110101 1110010 1110011 0101100
1000111 1100101 1110010 1110010 1101001 1110100