This module contains a class which can be used as a super class for all classes that need to implement the Singleton design pattern.


class Singleton(*p, **k)[source]

This class allows Singleton objects The __new__ method is overriden to force Singleton behaviour. The Singleton is created for the lowest subclass. Usage:

from taurus.core.util.singleton import Singleton

class MyManager(Singleton):

    def init(self, *args, **kwargs):
        print("Singleton initialization")

command line:

>>> manager1 = MyManager()
Singleton initialization

>>> manager2 = MyManager()

>>> print(manager1,manager2)
<__main__.MyManager object at 0x9c2a0ec>
<__main__.MyManager object at 0x9c2a0ec>

Notice that the two instances of manager point to the same object even though you tried to construct two instances of MyManager.


although __new__ is overriden __init__ is still being called for each instance=Singleton()

(more info)