Created
June 9, 2016 11:52
-
-
Save kurozumi/904e588369231971575892f48198c865 to your computer and use it in GitHub Desktop.
【Python】Pythonでシングルトンを実装する方法
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 = None | |
# __new__は__init__の前に実行されるのでここでインスタンスが生成されているか確認する | |
def __new__(cls, *args, **kwargs): | |
if cls.__instance is None: | |
cls.__instance = object.__new__(cls) | |
return cls.__instance | |
def __init__(self, name): | |
self.__name = name | |
@property | |
def name(self): | |
return self.__name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment