This module provides the AttrDict class, which allows you to access dict members via attribute access, allowing similar syntax to javascript objects. For example:
d = {"foo": 1, "bar": {"sub": {"subsub": 2}}}
ad = AttrDict(d)
assert ad["foo"] == ad["foo"]
assert ad.foo == 1
assert ad.bar.sub.subsub == 2
Note that AttrDict simply provides a view on the native dict. That dict can be obtained using the plus operator like so:
ad = AttrDict(d)
assert +ad is d
This allows use of native dict methods such as d.update() or d.items(). Note that attribute access binds more tightly than plus, so brackets will usually need to be used, eg: (+ad.bar).items().
An AttrDict can also be directly used as an iterator (for key in attrdict: ...) and as a container (if key in attrdict: ...).
del self.attr
del self[item]
self == other
self.attr
self[attr]
Provide an AttrDict view of a dictionary.
Parameters: | base – dictionary/list to be viewed |
---|
self != other
View underlying dict object
self.attr = val
self[item] = val