Python 单例模式
2018-11-08 本文已影响43人
爱吃猫的老虎
# 单例模式
class Boss(object):
__instance = None
__first = False
def __new__(cls, *args, **kwargs):
if not cls.__instance:
cls.__instance = object.__new__(cls)
return cls.__instance
def __init__(self, name):
if not self.__first:
self.name = name
self.__first = True