Exercises

These exercises check whether you can read and write small records without turning objects into a separate subject.

Exercise: Read an attribute

Given this object:

config.learning_rate

What is the attribute name?

Answer it first, then check.

Hint

In object.attribute, the attribute is the part after the dot.

Solution

The attribute name is:

learning_rate

It is the part after the dot in config.learning_rate.

Exercise: Create a dataclass object

Edit the code so it prints Ada 8.

Instantiate a record

Runs locally with Python in your browser.

Ready to run.

Hint

Call the dataclass like a function:

ScoreRecord(name="Ada", score=8)
Solution

One fix is:

The dataclass call creates an object with the given field values.

Exercise: Call a method

What expression calls the passed method on record?

Answer it first, then check.

Hint

Methods need parentheses when you call them.

Solution

Use:

record.passed()

The dot selects the method from the object. The parentheses call it.

Exercise: Read a type hint

In this function header, what is the return type?

def scale(values: list[float], factor: float) -> list[float]:

Answer it first, then check.

Hint

The return type is the hint after the arrow.

Solution

The return type is:

list[float]

It appears after the arrow in the function header.

Exercise: Use a configuration record

Edit the code so it prints [1.0, 1.5, 2.0].

Pass config into a computation

Runs locally with Python in your browser.

Ready to run.

Hint

Replace None with:

ExperimentConfig(start=1.0, step=0.5, epochs=3)
Solution

One fix is:

The function reads the settings from the configuration object and returns three values: [1.0, 1.5, 2.0].