You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python模块间传递Config对象的最优方案(无需类包装)

无需类包装的Python包配置传递方案

方案1:全局配置模块共享

通过在包内维护一个全局Config实例,让用户初始化后自动被所有函数复用,这是最直接的实现方式。

包内实现

在pkg/config.py中添加全局变量与初始化函数:

# pkg/config.py
class Config:
    def __init__(self, config_path):
        # 原Config类的加载逻辑
        self.config_path = config_path
        # 读取配置文件等操作...

# 全局配置变量,初始为None
_global_config = None

def setup_config(config_path):
    """供用户调用的配置初始化入口"""
    global _global_config
    _global_config = Config(config_path)

def get_config():
    """内部模块调用的配置获取函数"""
    if _global_config is None:
        raise RuntimeError("请先调用setup_config初始化配置")
    return _global_config

在pkg/example_module.py中,函数直接获取全局配置:

# pkg/example_module.py
from pkg.config import get_config

def func1(arg):
    config = get_config()
    # 使用config与arg执行业务逻辑...

def func2(arg):
    config = get_config()
    # 业务逻辑...

def func3(arg):
    config = get_config()
    # 业务逻辑...

用户使用方式

只需初始化一次配置,后续直接调用函数:

from pkg.config import setup_config
from pkg import example_module

setup_config("config.yml")

example_module.func1("abc")
example_module.func2("xyz")
example_module.func3(123)

注意点

  • 多线程/多进程场景下,全局变量可能存在线程安全问题,可改用threading.local()存储线程独立的配置实例。
  • 全局配置为单例模式,若需切换配置,需重新调用setup_config覆盖原实例。

方案2:装饰器自动注入配置

用装饰器封装需要Config的函数,自动从全局获取配置并注入,保留原有函数的参数逻辑。

包内实现

在pkg/config.py中保留方案1的setup_config和get_config,新增装饰器:

# pkg/config.py
# ... 保留Config类、setup_config、get_config ...

def require_config(func):
    def wrapper(*args, **kwargs):
        config = get_config()
        # 将全局配置作为第一个参数传入原函数
        return func(config, *args, **kwargs)
    return wrapper

在pkg/example_module.py中给函数添加装饰器:

# pkg/example_module.py
from pkg.config import require_config

@require_config
def func1(config, arg):
    # 原函数逻辑无需修改
    pass

@require_config
def func2(config, arg):
    pass

@require_config
def func3(config, arg):
    pass

用户使用方式

与方案1完全一致,无需修改调用代码:

from pkg.config import setup_config
from pkg import example_module

setup_config("config.yml")

example_module.func1("abc")
example_module.func2("xyz")
example_module.func3(123)

优势

  • 原有函数参数定义无需大幅改动,仅需添加装饰器。
  • 装饰器可扩展支持临时传入自定义配置,覆盖全局配置。

方案3:上下文管理器临时切换配置

适合需要临时切换不同配置的场景,不会污染全局状态。

包内实现

在pkg/config.py中添加上下文管理器:

# pkg/config.py
# ... 保留Config类 ...

_global_config = None

def get_config():
    if _global_config is None:
        raise RuntimeError("请先初始化配置")
    return _global_config

class ConfigContext:
    def __init__(self, config_path):
        self.new_config = Config(config_path)
        self.old_config = None

    def __enter__(self):
        global _global_config
        self.old_config = _global_config
        _global_config = self.new_config
        return self.new_config

    def __exit__(self, exc_type, exc_val, exc_tb):
        global _global_config
        _global_config = self.old_config

用户使用方式

通过with语句临时切换配置:

from pkg.config import ConfigContext
from pkg import example_module

# 临时使用config.yml配置
with ConfigContext("config.yml"):
    example_module.func1("abc")
    example_module.func2("xyz")

# 切换到另一个配置执行
with ConfigContext("another_config.yml"):
    example_module.func3(123)

优势

  • 支持多配置切换,不影响全局状态。
  • 适合测试场景,临时替换配置验证逻辑。

内容的提问来源于stack exchange,提问作者Liz C.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 16:12:26