44. Collections: namedtuple
namedtuple creates tuple-like objects with named fields.
Example
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(3, 4)
print(p.x, p.y)
Benefits
- Lightweight alternative to classes
- Readable field names instead of indices
Wrap-Up
Use namedtuple when you need immutable, lightweight records.