【发布时间】:2020-03-17 17:01:44
【问题描述】:
我想从另一个 file2 运行一个 file1,同时更改其中一个变量。 像这样的:
file1.py
a = 1
b = 1
c = 1
# Then:
# Countless functions depending on a, b, c, etc., depending on each other in complicated ways.
# Results from these functions are plotted and exported
file2.py
# Pseudo-code:
import file1 with (a = 0) # runs file1, exporting all results, as if file1 had a = 0.
考虑到我在 file1.py 中有太多变量、复杂的函数和依赖项,最简单的方法是什么?
【问题讨论】:
-
您可以更改
file1.py以读取配置或至少从您可以先导入的通用模块中获取其变量吗?导入后更改file1.a只能影响在那之后使用a。file1.py可以使用一些重构。 -
是的,我可以。我也相信这是最好的选择,但不是那么 Pythonic。