beanbag.attrdict – Access dict members by attribute


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 == 1
assert == 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: (

An AttrDict can also be directly used as an iterator (for key in attrdict: ...) and as a container (if key in attrdict: ...).

class beanbag.attrdict.AttrDict(base=None)

del self.attr


del self[item]


self == other






Provide an AttrDict view of a dictionary.

Parameters:base – dictionary/list to be viewed

self != other


View underlying dict object

__setattr__(attr, val)

self.attr = val

__setitem__(item, val)

self[item] = val