Thursday, July 27, 2006

BorgCollective

The following is a Singleton base class for Python, called BorgCollective.py

By inhertiting this class into your own, you can make your class a Singleton.

class BorgCollective:

_objects = {}



def __init__( self ):



#get the name of the inheriting class

class_name = self.__class__



#if the dictionary doesn't exist, create it

if self._objects.has_key( class_name ) == 0 :

self._objects[ class_name ] = {}



self.__dict__ = self._objects[ class_name ]

No comments:

Post a Comment