Configuration Records

A configuration record stores settings for a computation.

In ML code, configuration values might include:

  • learning rate;
  • number of epochs;
  • batch size;
  • random seed;
  • input path;
  • output path.

Putting those values in one record makes the function call clearer.

Pass configuration as one object

Instead of this:

run_experiment(0.1, 3, 42)

prefer this when the settings need names:

The second version is longer, but it is less mysterious.

Pass a configuration record

Runs locally with Python in your browser.

Ready to run.

Keep configuration boring

A good configuration record is plain and explicit:

Avoid hiding computation in configuration objects. Their job is to name the settings a computation needs.

Milestone shape

By the end of this chapter, you should be comfortable reading code shaped like this:

The record names the settings. The function does the work.

Exercise: Why use a configuration record?

Which answer best describes the reason to pass a configuration record?

Choose the main reason

Select one choice, then check.

HintFocus on readability

Compare a call containing three unexplained positional values with a record whose fields name each setting.

SolutionConfiguration records name settings

The main benefit here is clarity: related settings travel together with visible field names. A dataclass does not automatically speed up the computation or enforce every type hint at runtime.