Check your understanding of the previous reading: functions that build, mutate, and convert dataclass instances.

Question 1

Which of the following statements is true?

Select every option that applies, then check.

  • Correct. Like parse_card in the reading, a function can build and return a dataclass instance.
  • Correct. Like area in the reading, a function can take a dataclass instance as a parameter.
  • Correct. Field access is just an expression like any other, so its value can be passed to a function call (like str(rect.length)).
  • Correct. A field's value is an ordinary expression, so it can be returned directly, just like any other value.

Question 2

Given the code below:

from dataclasses import dataclass
from bakery import assert_equal

@dataclass
class Wallet:
    cash: int
    cards: int

def add_wallets(left: Wallet, right: Wallet) -> __1__:
    return Wallet(
        left.cash + right.cash,
        __2__ + __3__
    )

assert_equal(add_wallets(Wallet(5, 10), Wallet(3, 2)), Wallet(8, 12))

The add_wallets function's return type (__1__) should be . The second argument to the returned Wallet (__2__ + __3__) should add and .

Word bank — not all words are used