Created
January 26, 2018 10:26
-
-
Save ferdef/94813e9ea12ad7183c5dc7bd15146aa6 to your computer and use it in GitHub Desktop.
Singleton Pattern 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 Singleton: | |
__instance = None | |
value = None | |
def __new__(cls): | |
if Singleton.__instance is None: | |
Singleton.__instance = object.__new__(cls) | |
return Singleton.__instance | |
instance_A = Singleton() | |
instance_A.value = 5 | |
instance_B = Singleton() | |
instance_B.value = 8 | |
print (instance_A.value, instance_B.value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment