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.
The lessonsin the order to take them.
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()isReview.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:
RatingErroris a subclass ofValueError. 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
fitand answers inpredict. 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 returnsself. 6.3 - hyperparameter
- A setting of a model chosen before training, not learned in
fit:kinKNNRegressor(k=2). 6.3 - predict
- The method that gives answers for new inputs, using what
fitlearned. 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-modelswithuv init. Insrc/tiny_models/, writeMeanPredictor,MedianPredictorandKNNRegressor(k=3), each withfit(X, y)that stores what it learns in attributes ending in_and returnsself, andpredict(X)that returns a list.predictbeforefitraises aNotFittedErrorof your own, a subclass ofValueError. Write aCardataclass (frozen,age: int,price: float), andload_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.