Created
December 29, 2014 15:08
-
-
Save gawen/0d9a1afd2b39e204da71 to your computer and use it in GitHub Desktop.
Singleton in Python
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 SingletonClass(object): | |
def __new__(cls): | |
if not cls._singleton: | |
self = super(SingletonClass, cls).__new__(cls) | |
# Real constructor | |
self._init() | |
cls._singleton = self | |
return cls._singleton | |
def _init(self): | |
self.value = 42 | |
# Call singleton | |
print SingletonClass().value | |
# > 42 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment