SchoolWhool
Quizzes and saved progress, freeSign in freeSign in
Python · Module 6

Objects and Classes

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

Lessons

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

Module material

Take these after the last lesson, in this order. Next on each page leads through them.

  1. Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
  2. Project: tiny-modelsStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
  3. NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
  4. Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
  5. Interview questions25 questions interviewers ask on objects and classes, from first principles to the follow-ups. The first 3 model answers are free to read; Pro opens the rest.

Glossary

Every term the module introduces, with the lesson that introduces it.

self
The conventional first parameter of an instance 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 that blocks field assignment and deletion, @dataclass(frozen=True). Nested mutable values can still change. With the default equality, its generated hash works only when every hashed field is 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