Convert nested Python dict to object?

后端 未结 30 2432
时光取名叫无心
时光取名叫无心 2020-11-22 09:28

I\'m searching for an elegant way to get data using attribute access on a dict with some nested dicts and lists (i.e. javascript-style object syntax).

For example:

30条回答
  •  时光取名叫无心
    2020-11-22 10:04

    How about this:

    from functools import partial
    d2o=partial(type, "d2o", ())
    

    This can then be used like this:

    >>> o=d2o({"a" : 5, "b" : 3})
    >>> print o.a
    5
    >>> print o.b
    3
    

提交回复
热议问题