Python 有没有库是可以把字典像属性那样操作的?

2018-06-01 20:56:02 +08:00
 jakeyfly

比如 dict_1 = {"a":1} 直接 dict_1.a 就会显示 1

我原来自己写了一个简单的,感觉功能不行,要赋值还要重写__setarrt__ 太复杂 怕弄错,有没有现成的库

3035 次点击
所在节点    问与答
16 条回复
niubee1
2018-06-01 21:18:33 +08:00
看看老的 tornado 的代码, 记得最早的版本里封装了一个, 很简单, 十几行代码就搞定了
coffeSlider
2018-06-01 21:53:36 +08:00
我的做法是,写个 class 设置__dict__ 为你的 dict
maemual
2018-06-01 21:56:31 +08:00
ObjectDict
jatsz
2018-06-01 22:23:05 +08:00
class AttrDict(dict):
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__

dict1 = AttrDict(dict_1)

dict1.a == 1
bravecarrot
2018-06-01 22:32:06 +08:00
写了一个贼丑的
```Python

class Duck(dict):
def __init__(self, *args, **kw):
return super(Duck, self).__init__(self, *args, **kw)

def __getattr__(self, key):
try:
return self[key]
except KeyError as e:
print e

def __setattr__(self, *args, **kw):
object.__setattr__(self, *args, **kw)
for k, v in self.__dict__.items():
if k not in self.keys():
self[k] = v


if __name__ == "__main__":
s = Duck()
# as a dict
s['a'] = 1
print s.a
# set and get attribute
s.c = [1,3]
print s.c
print s['c']
```
glasslion
2018-06-01 22:51:45 +08:00
just1
2018-06-01 22:54:09 +08:00
pip install python-box
jakeyfly
2018-06-01 23:42:40 +08:00
@bravecarrot
class FrozenDict:
def __new__(cls, arg):
if isinstance(arg, abc.Mapping):
return super().__new__(cls)
elif isinstance(arg, abc.MutableSequence):
return [cls(item) for item in arg]
else:
return arg

def __init__(self, mapping):
self.__data = {}
for key, val in mapping.items():
self.__data[key] = val

def __getattr__(self, name):
if hasattr(self.__data, name):
return getattr(self.__data, name)
else:
return FrozenDict(self.__data[name])

这我以前从书上学的,只读还行,要写入就要重写__setattr__ 不太会
jakeyfly
2018-06-01 23:43:04 +08:00
@glasslion 这个强无敌
bravecarrot
2018-06-01 23:56:34 +08:00
@jakeyfly 我觉得 @jatsz 的已经很强了

但是我的也能用呀(大笑)
jakeyfly
2018-06-02 00:29:06 +08:00
@bravecarrot 大佬你这个好像不能读嵌套的呀,哈
jakeyfly
2018-06-02 00:30:15 +08:00
@jatsz 大佬 你这是高端猴子补丁吗
param
2018-06-02 00:57:50 +08:00
秒变 JS
dacapoday
2018-06-02 08:40:36 +08:00
easydict
jatsz
2018-06-02 10:00:06 +08:00
@jakeyfly
这个是挺猴的,记不得在哪里看到的,不过也不能解决嵌套问题。

在项目中使用的是这个版本,能解决嵌套的问题:

class AttrDict(dict):
"""
A class to convert a nested Dictionary into an object with key-values
that are accessible using attribute notation (AttrDict.attribute) instead of
key notation (Dict["key"]). This class recursively sets Dicts to objects,
allowing you to recurse down nested dicts (like: AttrDict.attr.attr)
"""

# Inspired by:
# http://stackoverflow.com/a/14620633/1551810
# http://databio.org/posts/python_AttributeDict.html

def __init__(self, iterable, **kwargs):
super(AttrDict, self).__init__(iterable, **kwargs)
for key, value in iterable.items():
if isinstance(value, dict):
self.__dict__[key] = AttrDict(value)
else:
self.__dict__[key] = value
jakeyfly
2018-06-02 13:10:47 +08:00
@jatsz 嗯 我下了上面那位大佬的库 满好用的,我原先用递归的,经常出问题。还是用别人的轮子好~~~~~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/459686

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX