How to Convert Python dict to object?

假如我们有如下的数据:

1
d = {'a': 1, 'b': {'c': 2}, 'd': ["hi", {'foo': "bar"}]}

假如我需要访问c需要通过d.get('b').get('c')

如果能直接通过d.b.c去访问就好了

实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Struct:
'''The recursive class for building and representing objects with.'''
def __init__(self, obj):
for k, v in obj.iteritems():
if isinstance(v, dict):
setattr(self, k, Struct(v))
else:
setattr(self, k, v)
def __getitem__(self, val):
return self.__dict__[val]
def __repr__(self):
return '{%s}' % str(', '.join('%s : %s' % (k, repr(v)) for
(k, v) in self.__dict__.iteritems()))

使用:

1
2
s = Struct(d)
s.b.c