Python · Module 6

Objects and Classes3 lessons and their Study toolkit.

Watch each lesson, answer its quiz, then do its practice. After the last lesson: the module quiz, the project, and the interview questions.

3of 3 lessons ready
30quiz questions
16key terms
2self-checking notebooks

The lessonsin the order to take them.

3 lessons ready to study
  1. 6.1Classes and ObjectsBundle data and behaviour into objects, and read code that does. Open
  2. 6.2DataclassesDefine a class that mainly holds data in a few lines, with type hints. Open
  3. 6.3The Estimator PatternWrite a tiny model class with `fit` and `predict`, the shape every scikit-learn model has. Open

Glossaryevery term the module introduces.

Each with the lesson that introduces it.

self
The first parameter of a method: the object the method was called on. review.is_positive() is Review.is_positive(review). 6.1
attribute
A value stored on an object, reached with a dot: review.rating. vars(review) shows an object's attributes as a dictionary. 6.1
class
A definition of a new kind of object: class Review:. Calling it, Review(...), creates an instance. 6.1
inheritance
Defining a class from another, so that it starts with everything the other has: class VerifiedReview(Review):. 6.1
instance
An object created from a class: review = Review("kettle", 5, "…"). 6.1
method
A function defined inside a class, called on an object with a dot: review.tokens(). 6.1
special method
A method with two underscores on each side of its name, which Python calls itself: __init__, __repr__, __eq__, __add__. 6.1
subclass
A class defined from a base class: RatingError is a subclass of ValueError. 6.1
dataclass
A class whose __init__, __repr__ and __eq__ are written from its list of fields, with @dataclass. 6.2
decorator
A function applied to the class or function below it, written with @: @dataclass. 6.2
field
One attribute a dataclass lists, with a type hint: rating: int. 6.2
frozen
A dataclass whose objects can not be changed after they are created, @dataclass(frozen=True). Frozen objects are hashable. 6.2
estimator
An object that learns from data in fit and answers in predict. Every scikit-learn model is one. 6.3
fit
The method that learns from examples. It stores what it learned in attributes ending in _, and returns self. 6.3
hyperparameter
A setting of a model chosen before training, not learned in fit: k in KNNRegressor(k=2). 6.3
predict
The method that gives answers for new inputs, using what fit learned. 6.3

Module quiz15 questions across it all.

Take it after the last lesson. Your first pick on each question is the one that counts.

Project: tiny-modelsbuild it without a template.

Stated as a problem, with no step-by-step instructions. Working out the steps is the point.

The task

Create a project tiny-models with uv init. In src/tiny_models/, write MeanPredictor, MedianPredictor and KNNRegressor(k=3), each with fit(X, y) that stores what it learns in attributes ending in _ and returns self, and predict(X) that returns a list. predict before fit raises a NotFittedError of your own, a subclass of ValueError. Write a Car dataclass (frozen, age: int, price: float), and load_cars(path) that reads a CSV of cars into a list of them. Write tests with pytest for all of it, and make Ruff and mypy report nothing.

Notebooksthat check your answers.

Open them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.

  • Notebook 1 · Classes and DataclassesLessons 6.1 and 6.2 Courses plan
  • Notebook 2 · The Estimator PatternLessons 6.3 Courses plan

Referencefor revising and for interviews.

The cheat sheet is one page of the module’s terms, rules and gotchas. The interview questions come with model answers.