Skip to main content

How to Make a Python Data Class Read-Only with frozen=True

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A frozen data class can't be changed once it's made: every attempt to set one of its fields raises an error. That's useful for values that should never change by accident, such as a result your code returns. This guide freezes a small data class on Python 3.15.2 and shows the error.

Syntax​

Syntax
from dataclasses import dataclass


@dataclass(frozen=True)
class <ClassName>:
<field>: <type>
PartWhat it means
@dataclass(frozen=True)Makes a data class whose fields can't be assigned after it's created
<field>: <type>A field, written the same way as in any data class

The dataclasses documentation describes the option: "frozen: If true (the default is False), assigning to fields will generate an exception. This emulates read-only frozen instances."

The Example​

A Point holds two numbers, x and y. The script creates a point, prints it, then tries to move it by setting x.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

point.py
from dataclasses import dataclass


@dataclass(frozen=True)
class Point:
x: int
y: int


start = Point(1, 2)
print(start)
start.x = 5

The point.py script in a macOS-style code window: a Point data class marked frozen=True with int fields x and y, a point created at 1, 2 and printed, then a line that assigns 5 to its x field.

Step 2: Run It​

Terminal
uv run python point.py
Output
Point(x=1, y=2)
Traceback (most recent call last):
File "/Users/you/source/python-basics/point.py", line 12, in <module>
start.x = 5
^^^^^^^
File "<string>", line 16, in __setattr__
dataclasses.FrozenInstanceError: cannot assign to field 'x'

The terminal after uv run python point.py: Point(x=1, y=2), then a traceback pointing at start.x = 5 and ending in dataclasses.FrozenInstanceError: cannot assign to field 'x'.

Creating and printing the point worked. The assignment on line 12 didn't: frozen=True gave the class a __setattr__ method that refuses every change, which is why the traceback shows a __setattr__ in a file called <string>, code the decorator wrote for you. The command works unchanged in PowerShell.

The error is a FrozenInstanceError, which the documentation says "is a subclass of AttributeError", so an except AttributeError catches it too.

Make a Changed Copy Instead​

When you need a point with a different x, make a new one. dataclasses.replace copies an object and changes the fields you name. Save this as point_copy.py:

point_copy.py
from dataclasses import dataclass, replace


@dataclass(frozen=True)
class Point:
x: int
y: int


start = Point(1, 2)
print(start)
moved = replace(start, x=5)
print(moved)
print(start)
Terminal
uv run python point_copy.py
Output
Point(x=1, y=2)
Point(x=5, y=2)
Point(x=1, y=2)

moved is a new point with x=5, and start is still Point(x=1, y=2).

Common Mistakes​

MistakeWhat happensDo this instead
Writing @dataclass(frozen=True) and then assigning a field in your own codedataclasses.FrozenInstanceError: cannot assign to field 'x'Create a new object with replace(), or don't freeze the class if it really must change
Writing your own __setattr__ in a frozen data classThe docs say "If __setattr__() or __delattr__() is defined in the class and frozen is true, then TypeError is raised"Let frozen=True provide them

FAQ​

Is a frozen data class completely unchangeable?​

Its fields can't be reassigned. But if a field holds something changeable, such as a list, the list itself can still be changed: a frozen Basket whose items field held ['apple'] printed Basket(items=['apple', 'pear']) after 'pear' was appended to that list. Freezing protects the field, not what's inside it.

Can I put a frozen data class in a set or use it as a dictionary key?​

Yes. A frozen data class with the default settings can be hashed: two equal points, Point(1, 2) and Point(1, 2), gave the same hash.

Conclusion​

frozen=True made Point read-only: printing worked, and start.x = 5 raised FrozenInstanceError: cannot assign to field 'x'. To get a different point, make a new one with dataclasses.replace.

Additional Resources​