class MusicPlayer(object):
"""单例模式"""
instance = None
flag_init = False
def __new__(cls, *args, **kwargs):
"""单例模式实现"""
if cls.instance is None:
cls.instance = super().__new__(cls)
print("New")
return cls.instance
def __init__(self):
# 每次都会初始化
#print("init")
# 只初始化1次
if MusicPlayer.flag_init == False:
MusicPlayer.flag_init = True
print("init")
return
if __name__ == '__main__':
player1 = MusicPlayer()
player2 = MusicPlayer()