Check your understanding of the previous reading: dataclass syntax and terminology, how fields are accessed, and when dataclass instances are equal.
Given the following dataclass:
from dataclasses import dataclass
@dataclass
class Assignment:
name: str
points: int
late: bool
The dataclass has fields. The type of the first field is . The name of the last field is . The name of the constructor function used to create instances is .
Count the lines in the body of the class: each line is one field. The first field's type is written after the colon on its line. The constructor function always has the same name as the dataclass itself.
The dataclass has 3 fields (name, points, late). The first field, name, has type str. The last field is late. The constructor function is Assignment, the same name as the dataclass.
Which of the following parts of a dataclass definition are required?
Select every option that applies, then check.
@dataclass above the header, Python treats the class as an ordinary class, not a dataclass.@dataclass is not built in; you must from dataclasses import dataclass before using it.class keyword; the decorator only adds extra behavior on top.Box (width, length) and Dog (name, age, is_fuzzy) from the reading don't share this shape.Match each term to the best definition. Not every term is used.
| Definition | Term |
|---|---|
| A data structure for combining multiple kinds of data together to represent something | |
| A synonym for field | |
| A named bit of data inside of a dataclass | |
| The @dataclass above the header | |
| A specific, concrete version of a general dataclass with actual values | |
| A function automatically created when you define a dataclass that allows you to make instances |
Two terms are never used: Period (the dot used to access a field, e.g. ada.name - not defined among these statements) and Conditional (an if-statement concept unrelated to dataclasses).
@dataclass annotation is the Decorator.| Definition | Term |
|---|---|
| A data structure for combining multiple kinds of data together to represent something | Dataclass |
| A synonym for field | Attribute |
| A named bit of data inside of a dataclass | Field |
| The @dataclass above the header | Decorator |
| A specific, concrete version of a general dataclass with actual values | Instance |
| A function automatically created when you define a dataclass that allows you to make instances | Constructor |
Unused terms: Period, Conditional.
In the following code, what value will be printed?
from dataclasses import dataclass
@dataclass
class House:
color: str
address: str
print(House.color)
Select one answer, then check.
color: str) does not give the dataclass itself a color value - only an instance, created with the constructor, has actual field values. House.color raises an AttributeError.House.color raises an error instead.House doesn't set any defaults here.In the following code, what value will be printed?
from dataclasses import dataclass
@dataclass
class Tree:
height: float
age: int
kind: str
spruce = Tree(25.7, 12, 'Spruce')
print(spruce.height)
Select one answer, then check.
height is first, so it gets 25.7. Accessing spruce.height reads that value back.print can display any type of value, not only strings, and the code accesses .height, not .kind.spruce itself would show - but the code prints spruce.height, one specific field, which is perfectly legal.Tree.height) that fails.Which of the following are advantages of a dataclass?
Select every option that applies, then check.
my_box in the reading) shows all of its fields and their values.if statements do - they aren't a substitute for conditionals at all.from dataclasses import dataclass; if statements and function definitions need no import at all.Dog, can be used to create as many instances as you like, each with its own field values.