This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.smith
Recipients anthony, eric.smith, veky
Date 2019-11-13.21:14:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573679672.63.0.00239945391286.issue38758@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2019-11-13 21:14:32eric.smithsetrecipients: + eric.smith, veky, anthony
2019-11-13 21:14:32eric.smithsetmessageid: <1573679672.63.0.00239945391286.issue38758@roundup.psfhosted.org>
2019-11-13 21:14:32eric.smithlinkissue38758 messages
2019-11-13 21:14:32eric.smithcreate