Created
September 15, 2012 16:42
-
-
Save simpleton/3728765 to your computer and use it in GitHub Desktop.
Singleton implement by metaclass
This file contains hidden or 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): | |
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 | |
class Myclass(object): | |
__metaclass__ = Singleton |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment