Created
May 3, 2017 10:24
-
-
Save lucgiffon/45fd5731817eb1cb1f347d8940ee2ebe to your computer and use it in GitHub Desktop.
singleton
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(cls): | |
""" | |
Simple singleton implementation. | |
Usage: | |
@singleton | |
class A: | |
pass | |
a = A() | |
b = A() | |
# a == b | |
""" | |
instance = None | |
def class_instanciation_or_not(*args, **kwargs): | |
nonlocal instance | |
if not instance: | |
instance = cls(*args, **kwargs) | |
return instance | |
return class_instanciation_or_not |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment