class A:
__obj = None
__flag = False
def __new__(cls, *args, **kwargs):
if cls.__obj is None:
cls.__obj = object.__new__(cls)
return cls.__obj
def __init__(self,name):
if A.__flag == False:
print("init....")
self.name = name
A.__flag = True
a = A("a")
b = A("b")
print(a.name,b.name)
python学习之单例模式
最新推荐文章于 2023-06-27 17:30:23 发布