Message356554
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 |
|
Date |
User |
Action |
Args |
2019-11-13 21:14:32 | eric.smith | set | recipients:
+ eric.smith, veky, anthony |
2019-11-13 21:14:32 | eric.smith | set | messageid: <1573679672.63.0.00239945391286.issue38758@roundup.psfhosted.org> |
2019-11-13 21:14:32 | eric.smith | link | issue38758 messages |
2019-11-13 21:14:32 | eric.smith | create | |
|