Created
July 16, 2013 18:55
-
-
Save mitgr81/6011530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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