python的两种单例模式

基于模块的单例模式

在 Python 中,模块是天然的单例模式。因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,会直接加载 .pyc 文件,而不会再次执行模块代码。因此,可以将相关的类和实例定义在一个模块中,通过导入模块来获取唯一的实例。

py 复制代码
# singleton_module.py
class SingletonClass:
    def __init__(self):
        self.value = 0

    def increment(self):
        self.value += 1

# 创建唯一的实例
singleton_instance = SingletonClass()

然后在另一个文件中使用这个单例实例:

py 复制代码
# main.py
from singleton_module import singleton_instance

# 第一次调用
print(singleton_instance.value)  # 输出 0
singleton_instance.increment()

# 再次调用
from singleton_module import singleton_instance
print(singleton_instance.value)  # 输出 1

在 singleton_module.py 中定义了一个 SingletonClass 类,并创建了该类的一个实例 singleton_instance

main.py 中,无论导入多少次 singleton_instance,获取的都是同一个实例,因此对实例的操作会保持状态

基于装饰器的单例模式

使用装饰器可以在不修改原类代码的情况下,将类转换为单例类。装饰器会维护一个字典,用于存储类和其对应的实例,当第一次调用被装饰的类创建实例时,会将该实例存储在字典中,后续再次调用时,直接从字典中获取已创建的实例。

py 复制代码
# 入参是一个类,出参是一个函数
def singleton_decorator(cls):
    instances = {}

    def get_instance(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]

    return get_instance

@singleton_decorator
class MySingleton:
    def __init__(self):
        self.value = 0

    def increment(self):
        self.value += 1

# 创建实例
instance1 = MySingleton()
instance2 = MySingleton()

print(instance1 is instance2)  # 输出 True,表示两个实例是同一个对象
instance1.increment()
print(instance2.value)  # 输出 1

代码解释

singleton_decorator 是一个装饰器函数,它接受一个类作为参数,并返回一个 get_instance 函数

instances 是一个字典,用于存储类和其对应的实例

get_instance 函数会检查 instances 字典中是否已经存在该类的实例,如果不存在则创建一个新实例并存储在字典中,然后返回该实例。

MySingleton 类被 singleton_decorator 装饰

每次创建 MySingleton 类的实例时,实际上都会返回同一个实例

优缺点

基于模块的方式简单直接,适合简单场景

基于装饰器的方式更加灵活,可以应用于多个不同的类

相关推荐
用户83562907805115 小时前
Python 实现 PDF 文件加密与解密方法
后端·python
用户83562907805115 小时前
使用 Python 冻结与拆分 Excel 窗格教程
后端·python
你好潘先生1 天前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
Agent_大师1 天前
WebSocket 行情重连成功,K线缺口不会自动消失
python
荣码1 天前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
copyer_xyf1 天前
FastAPI 如何连接 MySQL
后端·python
apocelipes2 天前
常用编程语言和库的正则表达式性能对比
c语言·c++·python·性能优化·golang·开发工具和环境
用户8356290780512 天前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent2 天前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
咕白m6252 天前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python