Predict the output
What does the factory produce?
from dataclasses import dataclass
@dataclass
class Order:
id: int
status: str = "new"
total: int = 100
def make_order(id: int = 1, **kw: str) -> Order:
return Order(id=id, **kw) # type: ignore[arg-type]
print(make_order(7, status="shipped"))Hint 1 of 1
Which fields are overridden?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Order(id=7, status='shipped', total=100)
Correct. Only status is overridden; total keeps its default.
A factory supplies sensible defaults so each test sets only the fields it cares about. Here id is 7, status is overridden and total keeps its default of 100.
Why not the others?
- B
Order(id=1, status='shipped', total=100). id=7 is passed positionally. - C
Order(id=7, status='new', total=100). status is overridden. - D A TypeError. status is a valid field.