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