Recipes: Classes and objects¶
Short, task-focused how-tos for specific things you'll want to do when working with classes. Each recipe assumes you already know the basics — if a term surprises you, the Learn section is one click away.
Unlike the tutorials, the recipes don't have a recommended order. Pick whichever is useful right now.
Recipes in this guide¶
- Choose between
@dataclass,NamedTuple, and a plain class — a decision guide for the three ways to define a record-like type. - Validate attributes on assignment — using
__post_init__and@propertysetters to enforce invariants. - Make a class iterable or container-like — implementing
__iter__,__getitem__,__len__, and__contains__. - Avoid common class mistakes — mutable class attributes, deep inheritance,
super()ordering, and other traps.