How to convert a Python dictionary to postfix notation?


We have a dictionary, and want to convert it to a an object with postfix notation like

Convert to an immutable object

Use collections.namedtuple.

>>> from collections import namedtuple
>>> data = {"id": 1, "name": "foo"}
>>> namedtuple("Obj", data.keys())(**data)
Obj(id=1, name='foo')

Convert to a mutable object

Use types.SimpleNamespace.

>>> from types import SimpleNamespace
>>> SimpleNamespace(**{"id": 1, "name": "foo"})
namespace(id=1, name="foo")

See also: Convert a sequence to postfix notation