class Singleton(type):
def init(self, *args, *kwargs):
self.__instance = None
super().init(args, **kwargs)
def __call__(self, *args, **kwargs):
if self.__instance is None:
self.__instance = super().__call__(*args, **kwargs)
return self.__instance
else:
return self.__instance
Example
class Spam(metaclass=Singleton):
def init(self):
print('Creating Spam')
网友评论