[Zope] how to create a module global variable ?

Jerome Alet alet@librelogiciel.com
Thu, 3 Apr 2003 10:45:38 +0200


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