在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))
基本的用法就是这些了。
更多内容可以去查看我已经在PyPI上发布的any-singleton。

浙公网安备 33010602011771号