[Zope] Verrryy strange behavior (NameError)
Dieter Maurer
dieter@handshake.de
Fri, 6 Apr 2001 23:17:53 +0200 (CEST)
Andrew Athan writes:
> This is a multi-part message in MIME format.
>
> ------=_NextPart_000_00FD_01C0BEB2.C6C2B880
> Content-Type: text/plain;
> charset="Windows-1252"
> Content-Transfer-Encoding: 7bit
Please avoid MIME messages!
> ...
> _stateVariable=0
> .....
> class ....
> def fun()
> ....
> _stateVariable=1
>
>
> and this was causing me all kinds of grief. By changing the code to
>
> _stateVariable = {'a':0}
> ....
> class ...
> def fun()
> ...
> _stateVariable['a']=1
>
> everything works just fine. Now, can someone give me the shortcut to
> understand WHHHYYY?
When Python sees an assignment to a *variable*, it assumes
you want to define a new *local* variable.
You must explicitly declare it "global" to be able to change
the value of a global variable from a local context.
In the second case, you do not assign to a variable, but
modify an object (leaving all variable binding unchanged).
Dieter