1234567891011121314151617181920212223242526272829 |
- import abc
- class Singleton(abc.ABC):
- """ source: https://www.python.org/download/releases/2.2/descrintro/#__new__ """
- def __new__(cls, *args, **kwds):
- it = cls.__dict__.get("__it__")
- if it is not None:
- return it
- cls.__it__ = it = super(Singleton, cls).__new__(cls)
- it.init(*args, **kwds)
- return it
- @abc.abstractmethod
- def init(self, *args, **kwds):
- pass
- if __name__ == '__main__':
- class Test(Singleton):
- def init(self):
- print("Calling init")
- print(id(Test()))
- print(id(Test()))
|