Skip to content

Instantly share code, notes, and snippets.



Created Oct 19, 2011
What would you like to do?
Implementation of Singleton design pattern in Python (inspired from Alex Martelli talk)
# python -m doctest
class Singleton(object):
>>> s = Singleton()
>>> s #doctest: +ELLIPSIS
<hidden.Singleton object at 0x...>
>>> p = Singleton()
>>> p == s
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