Check your understanding of the previous reading: dataclass syntax and terminology, how fields are accessed, and when dataclass instances are equal.

Question 1

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 .

Word bank — not all words are used

Question 2

Which of the following parts of a dataclass definition are required?

Select every option that applies, then check.

  • Correct. Without @dataclass above the header, Python treats the class as an ordinary class, not a dataclass.
  • Correct. @dataclass is not built in; you must from dataclasses import dataclass before using it.
  • Correct. Like any class, it needs a name - this name also becomes the constructor function's name.
  • Correct. A dataclass is still defined with Python's class keyword; the decorator only adds extra behavior on top.
  • Not required. Fields can be named and typed however you like - Box (width, length) and Dog (name, age, is_fuzzy) from the reading don't share this shape.
  • Not required. A dataclass can have any number of fields, including zero or one.
  • Correct. As with any Python class, the body (the fields) must be indented under the header.
  • Not required. Dataclass bodies list fields; they don't need any control flow.

Question 3

Match each term to the best definition. Not every term is used.

DefinitionTerm
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

Question 4

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.

  • Correct. A field's type annotation (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.
  • Not quite. Python doesn't store the annotation as an accessible value on the class this way; accessing House.color raises an error instead.
  • Not quite. Fields have no default value unless you write one explicitly; House doesn't set any defaults here.
  • Not quite. No instance was ever created, so there is no house, red or otherwise, for Python to know about.
  • Not quite. An error does occur, but unit testing has nothing to do with it - the problem is accessing a field on the class instead of on an instance.

Question 5

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.

  • Correct. The constructor fills fields positionally, in the order they're defined - height is first, so it gets 25.7. Accessing spruce.height reads that value back.
  • Not quite. Fields are matched to constructor arguments in the order they're written in the class body, not alphabetically.
  • Not quite. print can display any type of value, not only strings, and the code accesses .height, not .kind.
  • Not quite. That is what printing spruce itself would show - but the code prints spruce.height, one specific field, which is perfectly legal.
  • Not quite. It's the reverse: accessing a field on an instance is exactly how fields are meant to be read; it's accessing a field on the dataclass itself (like Tree.height) that fails.

Question 6

Which of the following are advantages of a dataclass?

Select every option that applies, then check.

  • Correct. That's the core idea: bundling related fields, possibly of different types, into one type.
  • Correct. Printing an instance (like my_box in the reading) shows all of its fields and their values.
  • Not quite. Dataclasses and functions do different jobs (bundling data vs. running code) - one isn't a faster replacement for the other.
  • Not quite. Dataclasses don't make decisions the way if statements do - they aren't a substitute for conditionals at all.
  • Not quite. Dataclasses always need from dataclasses import dataclass; if statements and function definitions need no import at all.
  • Correct. One definition, like Dog, can be used to create as many instances as you like, each with its own field values.