Created
August 24, 2019 07:10
-
-
Save Madoshakalaka/24da1042001f90dbb88866146606fd15 to your computer and use it in GitHub Desktop.
pythonic singleton class
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 ClassName: | |
_instance = None | |
def __init__(self, arg1, arg2, arg3): | |
if ClassName._instance is None: | |
# do initialization here | |
ClassName._instance = self | |
def __new__(cls, **kwargs): | |
if cls._instance is None: | |
_instance = super().__new__(cls, **kwargs) | |
else: | |
_instance = cls._instance | |
return _instance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment