Created
July 6, 2018 15:10
-
-
Save santosh/48af54b3bccbbdc4bd640565f55abe29 to your computer and use it in GitHub Desktop.
Singleton example in #Python
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Singleton is something I have already used in Game Development with Unity. | |
class MySingleton(object): | |
"""docstring for MySingleton""" | |
_instance = None | |
def __new__(self): | |
if not self._instance: | |
self._instance = super(MySingleton, self).__new__(self) | |
self.y = 10 | |
return self._instance | |
x = MySingleton() | |
print(x.y) | |
x.y = 20 | |
# z.y have the value from class we instanced before | |
z = MySingleton() | |
print(z.y) | |
# See https://stackoverflow.com/q/6760685/939986 for more |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment