Skip to content

Instantly share code, notes, and snippets.

@gipi
Created October 19, 2011 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gipi/1299558 to your computer and use it in GitHub Desktop.
Save gipi/1299558 to your computer and use it in GitHub Desktop.
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