[python] Singleton pattern

This is what I found from internet, when you need singleton object in global.



class Singleton(object):

    _singletons = {  }

    def __new__(cls, *args, **kwds):

        if not cls._singletons.has_key(cls):

            cls._singletons[cls] = object.__new__(cls)

        return cls._singletons[cls]


So this is how to use this.


class SingletonQueue(Singleton):

    def __init__(self, queue=None):

        if queue != None:

            self._queue = queue

    def get(self):

        return self._queue.get()

    def put(self, item):

        return  self._queue.put(item)

    def size(self):

        return self._queue.qsize()

t = SingletonQueue()

a = SingletonQueue() # a == t