[Zope] how to create a module global variable ?

Stefan H. Holek stefan@epy.co.at
Thu, 03 Apr 2003 10:49:32 +0200


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/