Singleton

digraph inheritancec47d24a369 { bgcolor=transparent; rankdir=UD; ratio=compress; size="8.0, 12.0"; "Singleton" [color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,tooltip="This class allows Singleton objects"]; }
class Singleton(*p, **k)[source]

Bases: object

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.

Warning

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

init(*p, **k)[source]