Skip to content
Advertisement

How to convert a nested Python dict to object?

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:

JavaScript

Should be accessible in this way:

JavaScript

I think, this is not possible without recursion, but what would be a nice way to get an object style for dicts?

Advertisement

Answer

Update: In Python 2.6 and onwards, consider whether the namedtuple data structure suits your needs:

JavaScript

The alternative (original answer contents) is:

JavaScript

Then, you can use:

JavaScript
Advertisement