Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created June 9, 2016 11:52
Show Gist options
  • Save kurozumi/904e588369231971575892f48198c865 to your computer and use it in GitHub Desktop.
Save kurozumi/904e588369231971575892f48198c865 to your computer and use it in GitHub Desktop.
【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