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'])
>>> obj = Obj(*data)
>>> obj.name
'foo'

Convert to a mutable object

Use types.SimpleNamespace.

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

See also: Convert a dictionary to postfix notation