# 单例模式 class B: __instance = None def __new__(cls, *args, **kwargs): if cls.__instance is None: obj = object.__new__(cls) cls.__instance = obj return cls.__instance def __init__(self, name, age): self.name = name self.age = age def func(self): print(self.name) a = B('alex', 80) # 实例化,传值 b = B('egon', 20) # 实例化,覆盖值 print(a) print(b) print(a.name) print(b.name)