Loading topic
__init__, __repr__ and __eq__, that you can read annotations such as x: int, and that you run Python 3.7 or newer. A few items marked below need 3.10. By the end you will know exactly which methods @dataclass adds, when it adds them, a4 min read__init__ and __repr__ do, and you can read annotations such as x: int. The examples run on Python 3.7 or newer.6 min read@dataclass and field defaults, and that you understand the difference between mutable and immutable objects. You need Python 3.7 or newer. One behavior described below changed in Python 3.11, and4 min read@dataclass, know why mutable defaults need default_factory, and have Python 3.7 or newer. The per-field kw_only option needs Python 3.10 or newer.4 min read@dataclass, declare defaults, and use field(). You should also know the difference between mutable and immutable objects. The examples need Python 3.7 or newer.5 min read@dataclass, field() options such as init=False and repr=False, and the basics of __post_init__. You should also be comfortable with class inheritance and with the difference between instance and class attributes. InitVar itself works on any Python 3.7 or new5 min read@dataclass decides whether two instances are equal and how it lets you sort them. It assumes you know how == calls __eq__, how < calls __lt__, and how sorted() uses them. It also assumes you have read the chapters on fields and field() options.5 min readfrozen=True, which makes instances read-only. It also covers how eq, frozen and unsafe_hash together decide whether a dataclass can be a dictionary key or a set member.6 min read@dataclass, how defaults and field(default_factory=...) work, and what __post_init__ and InitVar do. You should also know the difference between a shallow copy and a deep copy.5 min read__init__, the order of repr output, and the order used for comparison. This chapter explains how the list is built and which errors come from i5 min readslots=True, which changes how instances store their attributes, and kw_only=True, which changes how the generated __init__ accepts arguments. It assumes you know how @dataclass generates __init__, how field() and default_factory wor5 min read@dataclass, you know what frozen=True and slots=True do, and you understand the difference between mutable and immutable objects. The goal is to decide which tool fits a given piece of data. Dataclasses are often the right choice, but not always.6 min read