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:
from dataclasses import field
@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?