class ObjectDict(dict):
def __getattr__(self, item):
val = self.__getitem__(item)
if isinstance(val, dict):
return self.__class__(**val)
elif isinstance(val, list):
return [self.__class__(**d) for d in val]
else:
return val
if __name__ == '__main__':
data = {
'id': 1,
'name': '苹果',
'solder': [
{
'id': 1,
'name': 'a'
},
{
'id': 2,
'name': 'b'
}
]
}
result = ObjectDict(data)
# result.solder[1].name = 'jack'
print(result.solder[1].name)