Python Glossary Русский

Python 3.7+Exceptionsexception

dataclasses.FrozenInstanceError

module exceptions

Raised on an attempt to set or delete a field of a frozen dataclass (@dataclass(frozen=True)).

Syntax

raise dataclasses.FrozenInstanceError

It subclasses AttributeError, so a broad except AttributeError swallows it silently — catch FrozenInstanceError by name if you want to tell a write to a frozen object apart from a misspelled attribute. Also, frozen=True only blocks setting and deleting attributes on the instance itself: a list or dict stored in a field can still be mutated in place, so the immutability is shallow. To get a different value in a field, build a modified copy with dataclasses.replace().

Examples

import dataclasses
try:
    raise dataclasses.FrozenInstanceError
except dataclasses.FrozenInstanceError as e:
    print(type(e).__name__)   # → FrozenInstanceError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy