Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【Python】Pythonでシングルトンを実装する方法
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