Created
October 19, 2011 20:25
-
-
Save gipi/1299558 to your computer and use it in GitHub Desktop.
Implementation of Singleton design pattern in Python (inspired from Alex Martelli talk)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python -m doctest singleton.py | |
class Singleton(object): | |
""" | |
>>> s = Singleton() | |
>>> s #doctest: +ELLIPSIS | |
<hidden.Singleton object at 0x...> | |
>>> p = Singleton() | |
>>> p == s | |
True | |
""" | |
def __new__(cls, *a, **k): | |
if not hasattr(cls, '_inst'): | |
cls._inst = super(Singleton, cls).__new__(cls, *a, **k) | |
return cls._inst |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment