Created
November 22, 2012 11:37
-
-
Save giupo/4130708 to your computer and use it in GitHub Desktop.
Python Singleton
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
## Metaclass for singletons | |
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 | |
## Real instance ... | |
class MySingleton(object): | |
__metaclass__ = Singleton | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment