Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
从模块 A 中,有没有办法覆盖在模块 B 的顶层定义的变量?只是为了让我想通过对该变量的临时调整来运行模块 B 的代码,比如在单元测试中?
是的,只需分配给模块上的属性:
import moduleB moduleB.some_variable = 'foo'
请注意,已经some_variable 作为全局导入的任何其他代码都不会更新。
some_variable