From: Stas Z. <sta...@us...> - 2005-09-14 14:42:38
|
Update of /cvsroot/gmailagent/GA-Qgcm In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4925 Added Files: BorgSingleton.py Log Message: added singleton class --- NEW FILE: BorgSingleton.py --- """ Borg and more than one module, Jan Wender, 2001/10/11 If you want to use the Borg pattern from more than one module, it works as follows: Extract the Borg Pattern into a module and a class of its own: borg.py: class Borg: __shared_state = {} # or org.python.core.PyStringMap in jython def __init__(self): self.__dict__ = self.__shared_state Let your class in a separate module inherit from Borg and call Borg's initializer in your __init__: import borg class Test(borg.Borg): def __init__(s): borg.Borg.__init__(s) Although sounding like a swedish chef, it works now. The state is shared across all modules using the Test class. """ class Borg: __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state # and whatever else you want in your class -- that's all! |