gpt4 book ai didi

python - 如何覆盖导入的 Python 模块中的常量?

转载 作者:IT老高 更新时间:2023-10-28 21:18:41 27 4
gpt4 key购买 nike

在我的应用程序中,我使用 example 包中的模块,称为 examplemod

我的应用:

from example import examplemod
examplemod.do_stuff()

它像这样在 example 中导入另一个模块。

examplemod.py:

from example import config
# uses config
# then does stuff

config 使用常量。

config.py:

CONSTANT = "Unfortunate value"

我想在我的应用程序中使用 examplemod 时覆盖此常量(将其设置为 CONSTANT = "Better value"),我更喜欢不要修改底层模块,所以我不必维护自己的包。我该怎么做?

最佳答案

是的,但它只能在完全限定的模块访问路径下按预期工作:

import example
example.examplemod.config.CONSTANT = "Better value"
example.examplemod.do_stuff()

关于python - 如何覆盖导入的 Python 模块中的常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12496089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com