Skip to content

Instantly share code, notes, and snippets.

@gipi

gipi/singleton.py

Created Oct 19, 2011
Embed
What would you like to do?
Implementation of Singleton design pattern in Python (inspired from Alex Martelli talk)
# 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