Created
September 7, 2022 15:44
-
-
Save syedjafer/010caf3f4eb59ab35aa02365629d2a5c to your computer and use it in GitHub Desktop.
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
def singleton(Class): | |
instances = {} | |
def getInstances(*args, **kwargs): | |
if Class not in instances: | |
instances[Class] = Class(*args, **kwargs) | |
return instances[Class] | |
return getInstances | |
@singleton | |
class Test: | |
def __init__(self): | |
self.val = 10 | |
obj1 = Test() | |
print(obj1.y) | |
obj1.y = 30 | |
obj2 = Test() | |
print(obj2.y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment