比如 dict_1 = {"a":1} 直接 dict_1.a 就会显示 1
我原来自己写了一个简单的,感觉功能不行,要赋值还要重写__setarrt__ 太复杂 怕弄错,有没有现成的库
1
niubee1 2018-06-01 21:18:33 +08:00
看看老的 tornado 的代码, 记得最早的版本里封装了一个, 很简单, 十几行代码就搞定了
|
2
coffeSlider 2018-06-01 21:53:36 +08:00 via Android
我的做法是,写个 class 设置__dict__ 为你的 dict
|
3
maemual 2018-06-01 21:56:31 +08:00
ObjectDict
|
4
jatsz 2018-06-01 22:23:05 +08:00 7
class AttrDict(dict):
__getattr__ = dict.__getitem__ __setattr__ = dict.__setitem__ dict1 = AttrDict(dict_1) dict1.a == 1 |
5
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'] ``` |
6
glasslion 2018-06-01 22:51:45 +08:00 3
|
7
just1 2018-06-01 22:54:09 +08:00 via Android 1
pip install python-box
|
8
jakeyfly OP @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__ 不太会 |
10
bravecarrot 2018-06-01 23:56:34 +08:00 via iPhone
|
11
jakeyfly OP @bravecarrot 大佬你这个好像不能读嵌套的呀,哈
|
13
param 2018-06-02 00:57:50 +08:00 via Android
秒变 JS
|
14
dacapoday 2018-06-02 08:40:36 +08:00 via iPhone
easydict
|
15
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 |