jakeyfly
V2EX  ›  问与答

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

  •  
  •   jakeyfly · Jun 1, 2018 · 3861 views
    This topic created in 3083 days ago, the information mentioned may be changed or developed.

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

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

    16 replies  •  2018-06-02 13:10:47 +08:00
    niubee1
    niubee1
    Jun 1, 2018
    1
    看看老的 tornado 的代码, 记得最早的版本里封装了一个, 很简单, 十几行代码就搞定了
    coffeSlider
    coffeSlider
    Jun 1, 2018 via Android
    2
    我的做法是,写个 class 设置__dict__ 为你的 dict
    maemual
    maemual
    Jun 1, 2018
    3
    ObjectDict
    jatsz
    jatsz
    Jun 1, 2018❤️7
    4
    class AttrDict(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__

    dict1 = AttrDict(dict_1)

    dict1.a == 1
    bravecarrot
    bravecarrot
    Jun 1, 2018
    5
    写了一个贼丑的
    ```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']
    ```
    just1
    just1
    Jun 1, 2018 via Android❤️1
    7
    pip install python-box
    jakeyfly
    jakeyfly
    OP
    Jun 1, 2018
    8
    @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
    jakeyfly
    OP
    Jun 1, 2018
    9
    @glasslion 这个强无敌
    bravecarrot
    bravecarrot
    Jun 1, 2018 via iPhone
    10
    @jakeyfly 我觉得 @jatsz 的已经很强了

    但是我的也能用呀(大笑)
    jakeyfly
    jakeyfly
    OP
    Jun 2, 2018
    11
    @bravecarrot 大佬你这个好像不能读嵌套的呀,哈
    jakeyfly
    jakeyfly
    OP
    Jun 2, 2018
    12
    @jatsz 大佬 你这是高端猴子补丁吗
    param
    param
    Jun 2, 2018 via Android
    13
    秒变 JS
    dacapoday
    dacapoday
    Jun 2, 2018 via iPhone
    14
    easydict
    jatsz
    jatsz
    Jun 2, 2018
    15
    @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
    jakeyfly
    OP
    Jun 2, 2018
    16
    @jatsz 嗯 我下了上面那位大佬的库 满好用的,我原先用递归的,经常出问题。还是用别人的轮子好~~~~~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1139 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 23:16 · PVG 07:16 · LAX 15:16 · JFK 18:16
    ♥ Do have faith in what you're doing.