Last active
March 1, 2022 03:16
-
-
Save Decision2016/12a58b8718a69dfc702877f1a784e334 to your computer and use it in GitHub Desktop.
[Python] Singleton with once initialization
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
class Singleton(object): | |
_instance_lock = threading.Lock() | |
__instance = None | |
def __new__(cls, *args, **kwargs): | |
if cls.__instance is None: | |
with Singleton._instance_lock: | |
cls.__instance = super( | |
Singleton, cls).__new__(cls) | |
else: | |
def init_pass(self): pass | |
cls.__init__ = init_pass | |
return cls.__instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment