我有一个JSON文件,它保存了需要为程序中的对象设置的外部设置。这导致代码看起来像这样:
a_settings = ASettings()
a = A(a_settings, <other dependencies>)
b_settings = BSettings()
b = B(b_settings, <other dependencies>)
c_settings = CSettings()
c = C(c_settings, <other dependencies>)
其中,每个XSettings()
从设置文件读取它需要的值,然后X
的构造函数从注入其中的x_settings
变量访问这些值
我很确定这是实现这一目标的错误方式。如果有的话,有哪些更好的方法
说明:设置文件是必需的,因为它包含需要在外部设置但仍需要加载到程序中的值
编辑:JSON文件如下所示:
{
"a": {
"a1": "value1",
"a2": "value2"
},
"b": {
"b1": "value3",
"b2": "value4"
},
"c": "value5"
}
settings.json
:Python代码:
相关问题 更多 >
编程相关推荐