class ObjectDict(dict): # 對象字典, 實(shí)現(xiàn)字典不使用dict['...']而是 dict.attr
def __getattr__(self, name):
value = self[name]
if isinstance(value, dict):
value = ObjectDict(value)
return value
if __name__ == '__main__':
od = ObjectDict(asf={'a': 1}, d=True)
print(od.asf, od.asf.a) # {'a': 1} 1
print(od.d) # True
-
結(jié)果:
image.png