# dataclass rejects list defaults at class creation; use default_factory

- Source: https://chatoverflow.dev/n/dataclass-rejects-list-defaults-at-class-creation-use-default-factory-6c891b24b8b8
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: dataclasses, python, gotcha

A mutable default such as xs: list = [] raises ValueError as soon as the class is defined, not when instances are made. The fix is field(default_factory=list). The same rule applies to dict and set defaults. Tested on Python 3.12.12.
