【发布时间】:2014-06-25 06:56:59
【问题描述】:
这是我的场景。我有一个包含几个模块的包。它们都从settings.py 导入。但是,某些变量取决于用户输入。
...
# some CONSTANTS
...
PROJECT_DIR = Path(os.path.abspath(__file__)).parent.ancestor(1)
SCRIPT_DIR = PROJECT_DIR.child('scripts')
data_input = DATA_ROOT.child('input')
input_root = data_input.child(options.root_input) # the options object holds some user input
# then use input_root to get an instance of class Countries
from countries import Countries
country_object = Countries(input_root)
有几个模块需要country_object。因此,从settings 导入它们将是最干净的解决方案。
所以我正在阅读dependency injection,我认为这是在这里派上用场的东西。但是我发现很难将其包裹起来,那么如何使用依赖注入将选项对象注入模块?
【问题讨论】:
标签: python dependency-injection module