Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

README.md

Named Tuples

Tuples with named fields, from the collections module. See namedtuple — Python docs.

Why use them?

Regular tuples require remembering what each index means — point[0] vs point.x. Named tuples give you readable field access while keeping the lightweight, immutable nature of a tuple.

Point2d = namedtuple('Point2d', 'x y')
p = Point2d(10, 20)
print(p.x)    # 10  — by name
print(p[0])   # 10  — by index still works

Run

python named_tuples.py