Why can't dataclasses have mutable defaults in their class attributes declaration?

PythonPython 3.x

Python Problem Overview


This seems like something that is likely to have been asked before, but an hour or so of searching has yielded no results. Passing default list argument to dataclasses looked promising, but it's not quite what I'm looking for.

Here's the problem: when one tries to assign a mutable value to a class attribute, there's an error:

@dataclass
class Foo:
    bar: list = []

# ValueError: mutable default <class 'list'> for field a is not allowed: use default_factory

I gathered from the error message that I'm supposed to use the following instead:

@dataclass
class Foo:
    bar: list = field(default_factory=list)

But why are mutable defaults not allowed? Is it to enforce avoidance of the mutable default argument problem?

Python Solutions


Solution 1 - Python

It looks like my question was quite clearly answered in the docs (which derived from PEP 557, as shmee mentioned):

> Python stores default member variable values in class attributes. Consider this example, not using dataclasses: > > class C: > x = [] > def add(self, element): > self.x.append(element) > > o1 = C() > o2 = C() > o1.add(1) > o2.add(2) > assert o1.x == [1, 2] > assert o1.x is o2.x > > Note that the two instances of class C share the same class variable x, as expected. > > Using dataclasses, if this code was valid: > > @dataclass > class D: > x: List = [] > def add(self, element): > self.x += element > > it would generate code similar to: > > class D: > x = [] > def init(self, x=x): > self.x = x > def add(self, element): > self.x += element > > This has the same issue as the original example using class C. That is, two instances of class D that do not specify a value for x when creating a class instance will share the same copy of x. Because dataclasses just use normal Python class creation they also share this behavior. There is no general way for Data Classes to detect this condition. Instead, dataclasses will raise a ValueError if it detects a default parameter of type list, dict, or set. This is a partial solution, but it does protect against many common errors.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGrahamView Question on Stackoverflow
Solution 1 - PythonGrahamView Answer on Stackoverflow