Advantages of Default Factory in Dataclasses
Alan Bawden
alan at csail.mit.edu
Tue Nov 16 18:24:43 EST 2021
David Lowry-Duda <david at lowryduda.com> writes:
...
For the same reason that the following code doesn't do what some people
might expect it to:
```python
def add_to(elem, inlist=[]):
inlist.append(elem)
return inlist
list1 = add_to(1)
list2 = add_to(2)
print(list1) # prints [1]
print(list2) # prints [1, 2], potentially confusing
```
Not only does it not print what "most people" expect. It also doesn't
print what _you_ expect! (But you made your point.)
More information about the Python-list
mailing list