python

We have a sequence such as a tuple or list, and want to convert it to a an object with named attributes in postfix notation (‘obj.name’).

Convert to an immutable object

Use collections.namedtuple.

>>> from collections import namedtuple
>>> data = (1, 'foo')
>>> obj = namedtuple('Obj', ['id', 'name'])(*data)
>>> obj.name
'foo'

Convert to a mutable object

Use types.SimpleNamespace.

>>> from types import SimpleNamespace
>>> data = (1, 'foo')
>>> obj = SimpleNamespace(**dict(zip(['id', 'name'], data)))
>>> obj.name
'foo'

See also: Convert a dictionary to postfix notation