Eric V. Smith <[email protected]> added the comment:
The problem is that what you wrote isn't what most people want. Here's your example without dataclasses. I've added an "append_to_x" method, which does the obvious thing: >>> class C: ... def __init__(self, x=[]): ... self.x = x ... ... def append_to_x(self, val): ... self.x.append(val) ... Now create two objects, and inspect their "x" properties: >>> a = C() >>> b = C() >>> a.x [] >>> b.x [] So far so good. Now append something to "a.x": >>> a.append_to_x(10) >>> a.x [10] And notice that "b.x" changes, too: >>> b.x [10] So the naive behavior isn't what you want. dataclasses is trying to prevent you from doing this. You should look at "mutable defaults", perhaps starting here (from a random Google search): https://blog.florimond.dev/python-mutable-defaults-are-the-source-of-all-evil ---------- _______________________________________ Python tracker <[email protected]> <https://bugs.python.org/issue38758> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
