Check your understanding of the previous reading: which operators work with dataclass instances, when two instances are equivalent, and how to annotate the types in a function that consumes and returns a dataclass.
Which of the following operators work with dataclass instances?
Select every option that applies, then check.
TypeError on dataclass instances.instance.field), not by position with square brackets.< also raise a TypeError - only equality and inequality work.When are two dataclass instances equivalent (==)?
Select one answer, then check.
== works fine on dataclass instances.== does not error, but two instances with matching class and fields do compare equal.Given the code below:
from dataclasses import dataclass
from bakery import assert_equal
@dataclass
class Fruit:
kind: str
sweetness: __1__
def is_sweet(fruit: __2__) -> __3__:
return fruit.sweetness > 10
assert_equal(is_sweet(Fruit("Orange", 15)), True)
assert_equal(is_sweet(Fruit("Lemon", 5)), False)
The sweetness field's type (__1__) should be . The fruit parameter's type (__2__) should be . The is_sweet function's return type (__3__) should be .
The test values 15 and 5 are whole numbers, so sweetness is an int. The parameter's type always matches the dataclass it's built from. is_sweet returns the result of a > comparison, which is always a bool.
sweetness is an int, the fruit parameter is a Fruit, and is_sweet returns a bool.