namedtuple

A tuple like (3, 4) doesn't say what each part means. A named tuple does: p.x and p.y instead of p[0] and p[1].

namedtuple("Point", ["x", "y"]) builds the type in one line. The typed form is a class that inherits from typing.NamedTuple and lists its fields with type hints. Both make real tuples, so indexing, unpacking and comparing with plain tuples all still work.

Like any tuple, a named tuple can't change. p._replace(x=10) returns a new one with one field changed. Named tuples are also hashable, so they work as dict keys.

They're a good fit for small records you return from a function or keep in a set. For records that need to change, or have methods and defaults, use a dataclass. The Classes chapter covers dataclasses.

Run it, then try p.x = 10 and read the error.