RE: [Zope] Verrryy strange behavior (NameError)
This is just normal python. When you want to assign to a global variable inside a function, you have to declare the variable as a global: _stateVariable = 0 class def func()
global _stateVariable
_stateVariable = 1
-Randy -----Original Message----- From: Andrew Athan [mailto:aathan-zope-list%REMOVEME@memeplex.com] Sent: Friday, April 06, 2001 1:01 PM To: zope@zope.org Subject: RE: [Zope] Verrryy strange behavior (NameError) I'm sending this in case my sagga helps some other relative newcomer. I suspect this problem has something to do with python1.5.2's (or Zope's hackery of...) handling of namespaces within modules & classes. What I needed was access to a "static global" variable to store some state for me. (No, I haven't looked into threads, their existance, or such issues in Zope). What I was doing was: _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? A.
Just to add to what Randall has said: When you declare a local variable in Python - that is, a variable that is declared within a function definition - it "shadows" any other variable of that name until the function exits. This means that if you assign a value to a variable of that name, Python creates a new one and uses that within the function. Many languages operate that way, not just Python. As Randall said, you can defeat this by using the "global" statement. It still isn't really "global", though. I believe it applies just within the same module as the function is defined in. Cheers, Tom P Randall F. Kern wrote - This is just normal python. When you want to assign to a global variable inside a function, you have to declare the variable as a global: _stateVariable = 0 class def func()
global _stateVariable
_stateVariable = 1
-Randy
Randy: Thanks for the note! Your explanation is one I understand clearly and one that occured to me early on. However here's what deeply confused me about what was/is going on and why I was not sure about the relationship to global vs local variables: (1) Access to the value of the global variable worked just fine, without declaration of the "global" keyword. (2) Access to the value of the global variable stopped working when a line of code setting a local variable was inserted many lines BELOW where it is accessed, and inside a block that never executes. (3) Access to the value of the global variable seemed to be dependent on the type of the data it stored. A. -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Randall F. Kern Sent: Friday, April 06, 2001 4:10 PM To: Andrew Athan; zope@zope.org Subject: RE: [Zope] Verrryy strange behavior (NameError) This is just normal python. When you want to assign to a global variable inside a function, you have to declare the variable as a global: _stateVariable = 0 class def func()
global _stateVariable
_stateVariable = 1
-Randy -----Original Message----- From: Andrew Athan [mailto:aathan-zope-list%REMOVEME@memeplex.com] Sent: Friday, April 06, 2001 1:01 PM To: zope@zope.org Subject: RE: [Zope] Verrryy strange behavior (NameError) I'm sending this in case my sagga helps some other relative newcomer. I suspect this problem has something to do with python1.5.2's (or Zope's hackery of...) handling of namespaces within modules & classes. What I needed was access to a "static global" variable to store some state for me. (No, I haven't looked into threads, their existance, or such issues in Zope). What I was doing was: _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? A. _______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
participants (3)
-
Andrew Athan -
Randall F. Kern -
Thomas B. Passin