how to create a module global variable ?
Hi, I'd want to create a variable which is shared by all instances of a Python class of my own. What I've done is that I've put the following line : _MySharedVar = None outside of any method or class definition, at the top of the python file which defines my class. I've done this because after having looked at Zope's sources, there are lots of _marker=[] there, and I guessed that's what I wanted to do. but then one method of my class has to initialize this variable if it is equal to None (meaning not yet initialized), but in the following code : def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes" the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment. I've tried to delete my product from the ZODB and restart Zope, delete and recreate my class instances, but the problem persists. What is the problem ? In Zope's sources, _marker is used directly without any "global" keyword use. Do I need to use the global keyword ? If yes then why ? If not then what did I do wrong ? thanks in advance Jerome Alet -- (c) 1928-???? The letters U, S, and A and the U.S.A. flag are the copyrighted property of Disney Enterprises Inc. Any infringement will be punished with the death penalty. Details : http://yro.slashdot.org/article.pl?sid=03/01/15/1528253&tid=123
You have to use global because you are assigning to _MySharedVar. The _markers are never assigned to so they do not need global. Stefan --On Donnerstag, 03. April 2003 10:45 +0200 Jerome Alet <alet@librelogiciel.com> wrote:
I'd want to create a variable which is shared by all instances of a Python class of my own.
What I've done is that I've put the following line :
_MySharedVar = None
outside of any method or class definition, at the top of the python file which defines my class.
I've done this because after having looked at Zope's sources, there are lots of _marker=[] there, and I guessed that's what I wanted to do.
but then one method of my class has to initialize this variable if it is equal to None (meaning not yet initialized), but in the following code :
def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes"
the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment.
I've tried to delete my product from the ZODB and restart Zope, delete and recreate my class instances, but the problem persists.
What is the problem ?
In Zope's sources, _marker is used directly without any "global" keyword use.
Do I need to use the global keyword ? If yes then why ? If not then what did I do wrong ?
-- Those who write software only for pay should go hurt some other field. /Erik Naggum/
On Thu, Apr 03, 2003 at 10:49:32AM +0200, Stefan H. Holek wrote:
You have to use global because you are assigning to _MySharedVar. The _markers are never assigned to so they do not need global.
I beg to disagree. The "if" is causing the exception, not the following line on which the assignment takes place. At least that's the line number which is reported (could it be false ?). With the global keyword it works but I still don't understand why... Thanks anyway.
def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes"
the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment.
Jerome Alet
see <http://www.faqts.com/knowledge_base/view.phtml/aid/4722/fid/241> but stop the discussion at this point...this is python country. -aj --On Donnerstag, 3. April 2003 11:14 Uhr +0200 Jerome Alet <alet@librelogiciel.com> wrote:
On Thu, Apr 03, 2003 at 10:49:32AM +0200, Stefan H. Holek wrote:
You have to use global because you are assigning to _MySharedVar. The _markers are never assigned to so they do not need global.
I beg to disagree. The "if" is causing the exception, not the following line on which the assignment takes place. At least that's the line number which is reported (could it be false ?).
With the global keyword it works but I still don't understand why...
Thanks anyway.
def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes"
the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment.
Jerome Alet
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
Ah! Then what Andreas said applies. ;-) Stefan --On Donnerstag, 03. April 2003 11:14 +0200 Jerome Alet <alet@librelogiciel.com> wrote:
On Thu, Apr 03, 2003 at 10:49:32AM +0200, Stefan H. Holek wrote:
You have to use global because you are assigning to _MySharedVar. The _markers are never assigned to so they do not need global.
I beg to disagree. The "if" is causing the exception, not the following line on which the assignment takes place. At least that's the line number which is reported (could it be false ?).
With the global keyword it works but I still don't understand why...
Thanks anyway.
def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes"
the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment.
Jerome Alet
-- Those who write software only for pay should go hurt some other field. /Erik Naggum/
Check out the "global" statement of Python -> www.python.org -> Documentation -aj --On Donnerstag, 3. April 2003 10:45 Uhr +0200 Jerome Alet <alet@librelogiciel.com> wrote:
Hi,
I'd want to create a variable which is shared by all instances of a Python class of my own.
What I've done is that I've put the following line :
_MySharedVar = None
outside of any method or class definition, at the top of the python file which defines my class.
I've done this because after having looked at Zope's sources, there are lots of _marker=[] there, and I guessed that's what I wanted to do.
but then one method of my class has to initialize this variable if it is equal to None (meaning not yet initialized), but in the following code :
def myMethod(self) : """My method.""" if _MySharedVar is None : _MySharedVar = "yes"
the "if" raises and UnboundLocalError, saying that I use _MySharedVar before assignment.
I've tried to delete my product from the ZODB and restart Zope, delete and recreate my class instances, but the problem persists.
What is the problem ?
In Zope's sources, _marker is used directly without any "global" keyword use.
Do I need to use the global keyword ? If yes then why ? If not then what did I do wrong ?
thanks in advance
Jerome Alet
-- (c) 1928-???? The letters U, S, and A and the U.S.A. flag are the copyrighted property of Disney Enterprises Inc. Any infringement will be punished with the death penalty. Details : http://yro.slashdot.org/article.pl?sid=03/01/15/1528253&tid=123
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
participants (3)
-
Andreas Jung -
Jerome Alet -
Stefan H. Holek