单例设计模式
class MusicPlayer(object):
__instance = None
__is_first = True
def __new__(cls, *args, **kwargs):
if cls.__instance is None:
cls.__instance = super().__new__(cls)
return cls.__instance
def __init__(self, name):
if self.__is_first:
self.name = name
self.__is_first = False
a = MusicPlayer("a")
b = MusicPlayer("b") # 第二次不会赋值
print(a is b) # True