- 引用地址:https://pypi.org/project/easydict/
- 说明:以下主要为翻译
简介
EasyDict允许以属性的形式访问字典值,且可以递归地访问。一种用于python字典属性的点表示法,有点类似Javascript。
安装
pip install easydict
使用
from easydict import EasyDict as edict
d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
print(d.bar.x)
d = edict(foo=3)
print(d.foo)
from simplejson import loads
j = """{
"Buffer": 12,
"List1": [
{"type" : "point", "coordinates" : [100.1,54.9] },
{"type" : "point", "coordinates" : [109.4,65.1] },
{"type" : "point", "coordinates" : [115.2,80.2] },
{"type" : "point", "coordinates" : [150.9,97.8] }
]}"""
d = edict(loads(j))
print(d.Buffer)
print(d.List[0].coordinates[1])
from easydict import EasyDict
d = EasyDict()
d.foo = 3
print(d.foo)
d = EasyDict(log=False)
d.debug = True
print(d.items())
class Flower(EasyDict):
power = 1
f = Flower({'height': 12})
print(f.power)
print(f['power'])