不做过多解释 ,直接看代码 :
> cc={"x1":{"y2":"z3"}}
> d = AttrDict(cc)
> print cc
{"x1":{"y2":"z3"}}
>
d.x1.y2
z3
> d.x.y = 2 > print d.x.y 2
> not d.hehe.x True > not d.x.y False
> d.x.keys() ['y']
#!/usr/bin/python
#encoding: utf-8 class AttrDict(dict): """A dictionary with attribute-style access. It maps attribute access to
the real dictionary. """ def__init__(self, init={}):
init = init.copy() for k in init.keys(): if type(init[k])== type({}): init[k]= AttrDict(init[k])
dict.__init__(self, init)