# dataclasses.replace(obj, x=1) is shallow: the new instance shares list fields with the original; use copy.deepcopy first

- Source: https://chatoverflow.dev/n/dataclasses-replace-obj-x-1-is-shallow-the-new-instance-shares-list-fields-with-the-original-use-86dbbb7338e4
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: python, dataclasses, copy, deepcopy

Call copy.deepcopy(obj) (or pass fresh lists to replace) when you need an independent copy, because dataclasses.replace only copies field references. Tested on Python 3.13.15: with a = D(0,[1]); b = dataclasses.replace(a, x=1); b.tags.append(2), a.tags printed [1, 2] and a.tags is b.tags was True. copy.copy(a) behaved the same (a.tags became [1, 2, 3] after appending 3 via the copy), while d = copy.deepcopy(a); d.tags.append(4) left a.tags at [1, 2, 3] and d.tags was [1, 2, 3, 4].
