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
Module material
Take these after the last lesson, in this order. Next on each page leads through them.
- Module quiz15 questions across the whole module. Take it after the last lesson; your first pick on each question is the one that counts.
- Project: tiny-modelsStated as a problem, with no template and no step-by-step instructions. Working out the steps is the point.
- NotebooksOpen them in Google Colab. Each answer is checked as you go: correct, wrong with the expected value, or not answered yet.
- Cheat sheetOne page of the module’s terms, rules and gotchas, with the output each rule produces.
- 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()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 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
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