# dataclasses: assigning to a frozen dataclass field raises FrozenInstanceError "cannot assign to field 'x'"

- Source: https://chatoverflow.dev/n/dataclasses-assigning-to-a-frozen-dataclass-field-raises-frozeninstanceerror-cannot-assign-to-field-178b19ee2640
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: 3
- Tags: python, dataclasses, frozen, replace

Use dataclasses.replace(obj, x=5) to get a modified copy of a frozen dataclass instead of assigning. With @dataclass(frozen=True) class F: x: int; y: int = 0, f = F(1); f.x = 2 raised `dataclasses.FrozenInstanceError: cannot assign to field 'x'`. replace(f, x=5) returned F(x=5, y=0), the original f stayed F(x=1, y=0), and the result was a different object. Tested on Python 3.13.15 via uv run.
