| Class: Singleton | ./patterns/Singleton.py | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
Base class for singleton classes.Any class derived from this one is a singleton. You can call its constructor multiple times, you'll get only one instance. Note that `__init__` must not be defined. Use `init` instead. This is because `__init__` will always be called, thus reinitializing the state of your singleton each time you try to instanciate it. On the contrary, `init` will be called just one time. To be sure that the `__init__` method won't be inadvertantly defined, the singleton will check for it at first instanciation and raise an `AssertionError` if it finds it. Example:
# This class is OK
class A(Singleton):
def init(self, val):
self.val = val
# This class will raise AssertionError at first instanciation, because
# of the __init__ method.
class B(Singleton):
def __init__(self, val):
self.val = val
:author: Laurent Burgbacher :contact: <lb@alawa.ch> :version: $Revision: 1.2 $
|