标签:div bsp single instance war 单例 加载 pre def
一、基于__new__
class Single: _instance = None def __new__(cls, *args, **kwargs): if not cls._instance: cls._instance = super(Single, cls).__new__(cls, *args, **kwargs) return cls._instance class Demon(Single): def foo(self): print("Hi") d1 = Demon() d2 = Demon() print(id(d1)) # 2110545800776 print(id(d2)) # 2110545800776
二、基于模块
1、重点:一个文件在一个项目中只加载一次
标签:div bsp single instance war 单例 加载 pre def
原文地址:https://www.cnblogs.com/wt7018/p/11366942.html