Created
October 23, 2023 14:12
-
-
Save dario61081/d48fbebb908b40f03ab3578d2c4c674c to your computer and use it in GitHub Desktop.
singleton in python decorator
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
def singleton(cls): | |
""" | |
Convert to singleton | |
""" | |
instance = [None] | |
def wrapper(*arg, **kwargs): | |
if instance[0] is None: | |
instance[0] = cls(*arg, **kwargs) | |
return instance[0] | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment