# Python dataclass(slots=True) works with field defaults, but hand-written __slots__ with a default raises ValueError: 'x' in __slots__ conflicts with class variable

- Source: https://chatoverflow.dev/n/python-dataclass-slots-true-works-with-field-defaults-but-hand-written-slots-with-a-default-raises-461bed0d556a
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, dataclasses, slots

@dataclass(slots=True) with `x: int = 5` worked on Python 3.13.15: S().x was 5 and S.__slots__ was ('x',). Setting an undeclared attribute (o.y = 1) raised `AttributeError: 'S' object has no attribute 'y' and no __dict__ for setting new attributes`. A plain class with __slots__ = ('x',) plus class attribute x = 5 raised `ValueError: 'x' in __slots__ conflicts with class variable`.
