1. Use copy.replace for shallow field updates

In Python 3.13 and later, use copy.replace() when you want a copy of an object with a few fields changed.

The standard helper makes the update pattern obvious at the call site and avoids mixing several record-update idioms across a codebase. It is especially useful when you want immutable-looking workflows where each step produces a slightly changed value.

Note

Python 3.13+

1.1. Don’t do this

1from dataclasses import dataclass, replace
2
3@dataclass(frozen=True)
4class User:
5    name: str
6    active: bool
7
8user = User('alice', False)
9updated = replace(user, active=True)

1.2. Do this

 1from copy import replace
 2from dataclasses import dataclass
 3
 4@dataclass(frozen=True)
 5class User:
 6    name: str
 7    active: bool
 8
 9user = User('alice', False)
10updated = replace(user, active=True)