Skip to content

Instantly share code, notes, and snippets.

@mitgr81
Created July 16, 2013 18:55
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 mitgr81/6011530 to your computer and use it in GitHub Desktop.
Save mitgr81/6011530 to your computer and use it in GitHub Desktop.
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
"""
Use this by defining Singleton as a class' metaclass
"""
class Foo(object):
__metaclass__ = Singleton
"""stuff"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment