Skip to content

Instantly share code, notes, and snippets.

@mshuffett
Created February 24, 2014 01:43
Show Gist options
  • Save mshuffett/9180387 to your computer and use it in GitHub Desktop.
Save mshuffett/9180387 to your computer and use it in GitHub Desktop.
Singleton metaclass
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]
class Singleton(_Singleton('SingletonMeta', (object,), {})): pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment