Search notes:

Python standard library: collections.namedtuple

collections.namedtuple allows to create objects with a given set of field names (attributes), somewhat similar to a struct in C.
from collections import namedtuple

NTcls = namedtuple(
          'NT'                     , # class name
          ['val1', 'val2', 'val3']   # field names
        )

print(type(NTcls))                                         # <class 'type'>

print([ d for d in dir(NTcls) if not d.startswith('__') ]) # ['_asdict', '_field_defaults', '_fields', '_make', '_replace', 'count', 'index', 'val1', 'val2', 'val3']

print(NTcls._fields)                                       # ('val1', 'val2', 'val3')

ntObj1 = NTcls('foo', 'bar', 'baz')
print(ntObj1)                                              # NT(val1='foo', val2='bar', val3='baz')

ntObj2 = NTcls( val2='world',val1='hello', val3='!' )
print(ntObj2)                                              # NT(val1='hello', val2='world', val3='!')

ntObj3 = NTcls._make( ['x', 'y', 'z'] )
print(ntObj3)                                              # NT(val1='x', val2='y', val3='z')

ntDict = ntObj3._asdict()
print(ntDict['val2'])                                      # y

See also

The tuple type
typing.NamedTuple is the typed version of collection.namedtuple.

Index