Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

Frozen Dataclasses vs. Slots: How to Choose for Python Data Models

Frozen dataclasses guard field assignment; slots constrain instance attributes. Learn how their distinct trade-offs affect Python data models, inheritance, weak references, and compatibility.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

frozen=True and slots=True solve different problems in Python dataclasses: freezing guards against ordinary field reassignment, while slots constrain instance attributes and can remove the usual per-instance dictionary. Use either—or both—when its trade-offs fit your model; neither makes objects universally immutable or guarantees a performance improvement.

What frozen dataclasses and slots each change

Choice What it does What it does not do
frozen=True Adds guards against assigning to or deleting instance fields after initialization. Such attempts raise FrozenInstanceError. It does not make referenced objects deeply immutable or prevent every possible mutation in Python.
slots=True Generates __slots__ and constrains instance attributes; absent an instance dictionary supplied by a base class or explicitly included, instances do not have the usual automatic __dict__. It does not protect existing field values from mutation or guarantee a particular memory or speed improvement.

The Python documentation states, “It is not possible to create truly immutable Python objects.” That is why “frozen” is best understood as a practical assignment guard, not a promise of absolute immutability. Python dataclasses documentation.

As an Amazon Associate I earn from qualifying purchases.

When to use frozen dataclasses

Choose frozen=True when a model should behave like a value whose fields are not ordinarily reassigned after construction. It can make accidental updates fail visibly and, with the default equality setting, affects whether dataclasses generate a hash.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Freezing only guards the field binding. For example, if a frozen object holds a list, code cannot normally replace the list field, but can still mutate the list’s contents. For value semantics, use immutable field types where appropriate or deliberately normalize inputs into immutable representations.

There is also a construction cost: the Python dataclasses documentation describes a “tiny performance penalty” because generated initialization cannot use simple assignment and must use __setattr__(). The documentation gives no numeric benchmark, so the practical impact should be evaluated in the application’s own workload rather than assumed.

When to use slots

Slots are useful when the set of attributes should be constrained or when an application creates enough instances that reducing per-instance overhead may matter. Their effect depends on the class hierarchy and workload; do not assume a universal memory saving or speedup.

  • Use slots when a fixed attribute set is a useful design constraint.
  • Measure memory and runtime in the actual application before adopting slots solely for optimization.
  • Check base classes: inheriting from a class without slots can restore an instance dictionary and weak-reference support.
  • Check weak-reference requirements. Slotted objects need a __weakref__ slot for weak references; dataclasses provide weakref_slot=True for this purpose, used with slots=True.

These mechanics and inheritance behavior are described in the Python data model documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Combining the options

A small coordinate value can use both options when its fields should not be rebound and its attribute set should remain fixed:

from dataclasses import dataclass

@dataclass(frozen=True, slots=True)
class Point:
    x: float
    y: float

point = Point(1.0, 2.0)
point.x = 3.0  # raises FrozenInstanceError

The two options remain independent: remove frozen=True if fields should be assignable, or remove slots=True if instances need a normal dynamic attribute dictionary. Combining them does not make mutable objects referenced by fields immutable.

Version and inheritance details to check

  • Dataclass slots support was added in Python 3.10. Code targeting earlier versions cannot rely on this decorator option.
  • Python 3.11 changed inherited-slot handling so generated slots do not duplicate names already present in base classes. Use dataclasses.fields() to enumerate dataclass fields; do not treat __slots__ as the field list.
  • slots=True returns a new class, which is a relevant compatibility detail for code that relies on class identity or decorator behavior.
  • Review the target Python version’s dataclass and data model documentation when supporting a range of interpreters, especially where inheritance and weak references are involved.

Hashing and equality are a separate design decision

With the default eq=True, combining it with frozen=True causes dataclasses to generate a hash. That is not a reason by itself to make a class frozen: hashability is useful only when equality and the values participating in the hash match the intended value semantics. Consider whether those field values can themselves change before relying on an instance as a dictionary key or set member. The dataclasses documentation describes the generated hash rules.

A practical choice checklist

  • Need ordinary field reassignment to fail? Consider frozen=True.
  • Need dynamic per-instance attributes? Avoid slots, or ensure a base class supplies the required dictionary.
  • Need weak references to instances? Account for __weakref__ support, using weakref_slot=True with dataclass slots where appropriate.
  • Need memory or runtime optimization? Benchmark representative application workloads and account for inherited classes.
  • Need compatibility with Python before 3.10? Do not use dataclass slots=True.
  • Need dataclass field names? Use dataclasses.fields(), not __slots__.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Further reading

For a broader treatment of Python data classes and memory-conscious object design, O’Reilly lists both subjects in Fluent Python, 2nd Edition by Luciano Ramalho, published in April 2022 (ISBN 9781492056348). Publisher page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.