python-黑魔法-python不一样的懒加载 yuan lang 2024-03-21 python python 懒加载一.一般方式懒加载12345678910111213class Singleton(object): _instance=None @classmethod def instance(clz): if clz._instance is None: clz._instance=Singleton() return clz._instanceprint(Singleton.instance()==Singleton.instance())# 弊端是直接Singleton()就不一样 二.__new__来实现懒加载1234567891011121314151617class Singleton(object): def __new__(clz): if not hasattr(clz,"_instance"): clz._instance=super(Singleton,clz).__new__(clz) return clz._instanceprint(Singleton()==Singleton())# 注意: __init__ 与 __new__区别与联系 # 1. 在调用__init__前是先调用__new__方法# 2. __new__一定会有return# 3. __new__可以自定义类的实例化 # 如: class PositiveInteger(int): def __new__(clz,value): return super(PositiveInteger,clz).__new__(clz,abs(value)) i=PositiveInteger(-3)# 4.__new__可以实现mateclass 三.new + mateclass实现懒加载123456789101112131415161718class TypeSingleton(type): def __new__(clz, name, base, attrs): # 这里可用用super 即object来创建 ,也可以直接tpye创建类 clz._instance = type.__new__(clz, name, base, attrs) return clz._instanceclass Singleton(metaclass=TypeSingleton): _instance = None @classmethod def instance(clz): return clz._instanceprint(Singleton.instance() == Singleton.instance())#注意:#1. object类是所有内的父类(包含type类)#2. type类是所有实例的父类(object也是type类创建) 四.高阶写法 多线程下懒加载12345678910111213141516171819import threadinglock=threading.Lock()class Singleton(object): def __new__(cls, *args, **kwargs): if not hasattr(cls,"_instance"): try: lock.acquire() if not hasattr(cls, "_instance"): cls._instance=super(Singleton,cls).__new__(cls,*args, **kwargs) finally: lock.release() return cls._instanceprint(Singleton() == Singleton())# 双重检查+单例