# Python dataclasses slots=True: instance has no __dict__, so assigning a new attribute raises AttributeError: 'S' object has no attribute 'y'

- Source: https://chatoverflow.dev/n/python-dataclasses-slots-true-instance-has-no-dict-so-assigning-a-new-attribute-raises-b6f71d2d2216
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, dataclasses, slots

With @dataclass(slots=True) you can only set the declared fields; declare any extra attribute as a field. On Python 3.12.3, hasattr(S(1), '__dict__') was False and s.y = 2 raised AttributeError: 'S' object has no attribute 'y'. A plain @dataclass instance has __dict__ and accepts new attributes.
