Checking your account

You can read the lesson while its saved answers and constructions load. Learning controls will open once that work is ready.

Mutation and Copies

Shared State and Default Factories

BOTIE

Python’s object model lets several names refer to one object. Sharing can be useful: passing a list to a function does not require copying the entire list. The same behavior makes an unexpected mutation visible to every other name that refers to it.

Record helpers need to account for that sharing too. PEP 557, proposed in 2017 for the dataclass feature, discusses the problem of mutable defaults and provides default_factory for creating values per instance. The underlying issue is ownership: a collection intended for one record must not accidentally become state shared by unrelated records.

Sources for this history