Skip to content

Instantly share code, notes, and snippets.

@simpleton
Created September 15, 2012 16:42
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 simpleton/3728765 to your computer and use it in GitHub Desktop.
Save simpleton/3728765 to your computer and use it in GitHub Desktop.
Singleton implement by metaclass
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
class Myclass(object):
__metaclass__ = Singleton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment