Skip to content

Instantly share code, notes, and snippets.

@ferdef
Created January 26, 2018 10:26
Show Gist options
  • Save ferdef/94813e9ea12ad7183c5dc7bd15146aa6 to your computer and use it in GitHub Desktop.
Save ferdef/94813e9ea12ad7183c5dc7bd15146aa6 to your computer and use it in GitHub Desktop.
Singleton Pattern in Python
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