Skip to content

Instantly share code, notes, and snippets.

@piotch
Last active August 29, 2015 14:08
Show Gist options
  • Save piotch/b5598babb10f874f9f50 to your computer and use it in GitHub Desktop.
Save piotch/b5598babb10f874f9f50 to your computer and use it in GitHub Desktop.
Singleton using __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 Blah(object):
__metaclass__ = Singleton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment