Named tuples were added in 2.6 for this purpose. Also see os.stat for a similar builtin example.
>>> import collections >>> Point = collections.namedtuple('Point', ['x', 'y']) >>> p = Point(1, y=2) >>> p.x, p.y 1 2 >>> p, p 1 2
In recent versions of Python 3 (3.6+, I think), the new
typing library got the
NamedTuple class to make named tuples easier to create and more powerful. Inheriting from
typing.NamedTuple lets you use docstrings, default values, and type annotations.
Example (From the docs):
class Employee(NamedTuple): # inherit from typing.NamedTuple name: str id: int = 3 # default value employee = Employee('Guido') assert employee.id == 3