Class Responsibilities

Learn about the class responsibilities in Python.

We'll cover the following

Overview

Which class is responsible for actually performing a test? Does the TrainingData class invoke the classifier on each KnownSample in a testing set? Or, perhaps, does it provide the testing set to the Hyperparameter class, delegating the testing to the Hyperparameter class? Since the Hyperparameter class has responsibility for the k value, and the algorithm for locating the k-nearest neighbors, it seems sensible for the Hyperparameter class to run the test using its own k value and a list of KnownSample instances provided to it.

It also seems clear the TrainingData class is an acceptable place to record the various Hyperparameter trials. This means the TrainingData class can identify which of the Hyperparameter instances has a value of k that classifies irises with the highest accuracy.

Emergent behavior

There are multiple, related state changes here. In this case, both the Hyperparameter and TrainingData classes will do part of the work. The system – as a whole – will change state as individual elements change state. This is sometimes described as emergent behavior. Rather than writing a monster class that does many things, we’ve written smaller classes that collaborate to achieve the expected goals.

This test() method of TrainingData is something that we didn’t show in the UML image. We included test() in the Hyperparameter class, but, at the time, it didn’t seem necessary to add it to TrainingData.

Here’s the start of the class definition:

Get hands-on with 1200+ tech skills courses.