在Python中用any-singleton实现单例模式单例模式

发布时间:2026/6/30 2:20:52

在Python中用any-singleton实现单例模式单例模式 我个人常用类似以下的代码来实现单例模式GLOBAL_KEY _my_coffee if GLOBAL_KEY not in globals(): # 初始化: globals()[GLOBAL_KEY] { cup_of: tea } coff globals()[GLOBAL_KEY] print(coff[cup_of]) # Output: tea上面的coff就是一个单例全局仅会初始化一次并总是同一个对象。至于其他的实现方法这里不多做赘述。大家可以去看看站内的Python中的单例模式的几种实现方式的及优化Any-singleton大部分情况下单例模式可以很容易得被实现并且正确运行。但总是要写一小段代码来实现就不那么方便也不易于管理。为此我就做了个简易的单例模式工具库——Any-singleton。Any-singleton提供了两大功能“创建单例”和“使函数仅运行一次”。创建单例我们仅需要调用singleton()并传入一个“唯一域名”和一个用于初始化的值就可以很快的创建一个单例对象from any_singleton import singleton tea singleton(my_project.main.coffee, tea)当然也可以不直接给一个值而是利用singleton()实例化一个对象作为单例的初始值from any_singleton import singleton my_range singleton(my_project.main.coffee, range, 123)当第二个参数为一个type时singleton()不会把该参数直接作为初始值而是将其结合后面的参数实例化再作为初始值。该单例的实例化过程在整个程序的生命周期将只会执行一次。为了消除歧义你还可以使用singleton_value()来替代singleton()使之无论第二个参数是不是type类型的都直接将其直接作为初始值使用from any_singleton import singleton_value class Tea: pass tea singleton_value(my_project.main.coffee, type(Tea))使函数仅运行一次使用once()装饰器来创建一个在整个程序生命周期里只会被调用一次的函数import tomllib from any_singleton import once, singleton once(my_project.initializations.init) def init(config_path: str) - None: with open(config_path, rb) as f: config singleton(my_project.globals.config, tomllib.load(f)) init(config.toml)或者使用run_once()装饰器来创建一个被once()装饰的函数并立即自动调用一次。import tomllib from any_singleton import run_once, singleton run_once( my_project.initializations.init, second_calling SecondCallingBehaviour.NoneToReturn, # 以下的参数将传递给被装饰的init()。 config.toml ) def init(config_path: str) - None: with open(config_path, rb) as f: config singleton(my_project.globals.config, tomllib.load(f))基本的用法就是这些了。

相关新闻