Skip to content

Instantly share code, notes, and snippets.

@giupo
Created November 22, 2012 11:37
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 giupo/4130708 to your computer and use it in GitHub Desktop.
Save giupo/4130708 to your computer and use it in GitHub Desktop.
Python Singleton
## Metaclass for singletons
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls,*args,**kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance
## Real instance ...
class MySingleton(object):
__metaclass__ = Singleton
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment