Loading topic
__init__, __repr__ and __eq__, that you can read annotations such as x: float, and that you run Python 3.7 or newer. By the end you will know which methods @dataclass writes for you and which it leaves out.2 min read__init__ and __repr__ do, can read type annotations such as x: int, and have seen the @dataclass decorator generate methods. It covers how a dataclass decides which names are fields, and how defaults change the generated `__i2 min read@dataclass syntax from earlier chapters.2 min read@dataclass with annotated fields and defaults. It also assumes you know why mutable defaults need default_factory. Here you will use field() to control how each field takes part in the generated methods.2 min read@dataclass, declare fields with defaults, and use field(). It also assumes you know that __init__ is the method Python calls to set up a new object.2 min read__post_init__ works and how field(init=False) excludes a field from the constructor. Sometimes the constructor needs a value that the finished object should not keep. A raw password, a unit multiplier, and a database connection used only to look something up are2 min read@dataclass generates methods, how to declare fields with defaults, and what __eq__ and __lt__ do on a plain class. Here you will see how the decorator compares instances and how to turn on sorting.2 min read@dataclass, field(), __post_init__, and the difference between mutable and immutable objects. Here you will see how frozen=True blocks changes, and how it decides whether instances can be dict keys or set members.2 min read@dataclass generates __init__, __repr__ and __eq__, and understand the difference between mutable and immutable objects. It covers four helper functions in the dataclasses module. They work on any dataclass instance, and none2 min read@dataclass with defaults and default_factory, and that you know how normal class inheritance works. The kw_only example needs Python 3.10 or newer.3 min read@dataclass, you know what __init__ and __repr__ do, and you are on Python 3.10 or newer. Both options below were added to @dataclass in 3.10.3 min readfield(), and read type annotations. It also assumes you know the difference between mutable and immutable objects. Here you will compare dataclasses with the main alternatives and pick one for a given job.2 min read