class A: # 静态字段和classmethod构成单例模式
__v = None
@classmethod
def get_instance(cls):
if cls.__v:
return cls.__v
else:
cls.__v = A()
return cls.__v
object1 = A.get_instance()
print(object1)
object2 = A.get_instance()
print(object2)
object3 = A.get_instance()
print(object3)
# 内存里对象只创建一次,省内存
# 应用场景: 数据库链接池