【发布时间】:2018-11-02 14:09:33
【问题描述】:
目前我正在使用json 将字典保存到配置文件中。我加载它以将其转换为dict,然后将其转换为SimpleNamespace,因为我更喜欢点符号来访问设置。为此,我像下面的例子一样加载它:
import json
from types import SimpleNamespace
SETTINGS = json.load(open("config.json", 'r'))
SETTINGS = SimpleNamespace(**SETTINGS)
但是,由于我目前正在将 dict 加载到 SimpleNamespace 中,因此它不会在配置文件中加载子字典。例如,如果我这样做:
SETTINGS.server_info.port
我得到错误:
AttributeError: 'dict' object has no attribute 'port'
我想知道如何将所有字典作为名称空间加载到命名空间中,以便能够在字典中一直使用点表示法。
【问题讨论】:
标签: python python-3.x dictionary