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

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use frozen=True when a dataclass should reject ordinary field reassignment, and slots=True when a fixed set of attributes is desirable or instance memory matters enough to measure. They address different concerns: frozen dataclasses guard assignment, while slots change how attributes are stored. You can use either option alone or combine them.

What frozen dataclasses and slots change

Choice What it does Useful when
frozen=True Adds guards against ordinary field assignment and deletion after initialization. Attempts raise FrozenInstanceError. Instances should behave like read-only values after construction.
slots=True Generates __slots__, constraining instance attributes and normally preventing an automatically created instance dictionary. The attribute set is fixed, or memory use across many instances is worth measuring.

Neither choice makes an object universally immutable or guarantees a particular performance improvement. Python’s dataclasses documentation says, “It is not possible to create truly immutable Python objects.”

When frozen=True is appropriate

A frozen dataclass is useful for value-like objects whose fields should not be rebound after creation. For example:

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

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

After constructing a Point, assigning point.x = 3 raises FrozenInstanceError. Deleting a field is guarded as well. These are assignment protections, not deep immutability: if a frozen field refers to a list, the reference cannot ordinarily be replaced, but the list’s contents can still change. For immutable value semantics, use immutable field types or deliberately normalize mutable inputs during construction.

Initialization and hashing trade-offs

The Python dataclasses documentation describes a “tiny performance penalty” for frozen=True: generated __init__() cannot use simple assignment to initialize fields and must use __setattr__(). The documentation gives no numeric benchmark, so the practical effect depends on the application and should not be converted into a general percentage.

With the default equality behavior, eq=True and frozen=True cause dataclasses to generate a hash. That does not make every instance safe or useful as a dictionary key: consider whether the values involved in hashing are themselves hashable and whether their state can change.

When slots=True is appropriate

Slots suit classes with a known, stable set of attributes. They prevent the normal automatic per-instance __dict__, unless one is available through a base class or explicitly included. This can constrain accidental attributes and may reduce memory use, especially for large populations of instances, but the result depends on the class hierarchy and workload. Measure your own application rather than assuming a fixed saving or speedup.

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

Slots also change how a class is created: the dataclass decorator returns a new class when slots=True is used. Review code that depends on class identity or on the usual instance dictionary before adopting the option.

Inheritance and weak references

A subclass of a class without slots can regain an instance dictionary and weak-reference support, so adding slots only to the dataclass may not produce the layout you expect. The Python data model documentation explains these inheritance and slot mechanics.

Slotted instances do not automatically support weak references unless __weakref__ is provided. Dataclasses offer weakref_slot=True for this purpose; use it only alongside slots=True and when weak references are needed. Check compatibility against the Python versions your project supports.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Combining frozen dataclasses and slots

Use both when you want assignment guards and a slotted layout:

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

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

This combination can fit small, value-like records with a fixed schema. It is not automatically the right choice for every dataclass: callers may rely on dynamic attributes, weak references, inheritance behavior, or a mutable field layout. Choose each option for its own reason, then check the combined class in its real hierarchy.

Python version compatibility

  • slots support for dataclasses was added in Python 3.10.
  • Python 3.11 changed handling of inherited slot names to avoid duplicating inherited fields in generated slots.
  • Do not use __slots__ to enumerate dataclass fields. Use dataclasses.fields(), as the dataclasses documentation advises.
  • For libraries or applications supporting multiple Python versions, verify other decorator and weak-reference options in the documentation for each target version.

A practical decision checklist

  • Choose frozen=True if ordinary reassignment should fail; do not treat it as deep immutability.
  • Choose slots=True if the attribute set should be constrained or measured memory use justifies changing instance layout.
  • Skip slots if dynamic attributes or an instance dictionary are part of the class’s intended interface.
  • Check base classes and subclasses for dictionaries, weak-reference needs, and inherited slots.
  • Use dataclasses.fields() for field introspection.
  • Benchmark representative object creation and memory use in your own workload if performance is the reason for the change.

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.