$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #256

Which fix removes the bug?

from dataclasses import dataclass

@dataclass
class Cart:
    items: list = []  # ValueError
  1. Add frozen=True to the decorator
  2. Annotate it as items: tuple = []
  3. Write it as items: list = list()
  4. Use field(default_factory=list)
dataclass refuses a mutable default because every instance would share it. default_factory builds a fresh list per instance.
Play Problem Solving ▸