Command Palette

Search for a command to run...

Hectal
Phase 8Intermediate9 of 18 in Python

Object-Oriented Python

Classes, objects, inheritance, the data model, dataclasses, enums and protocols: how to model real things in code and design classes that stay easy to change.

Until now you have worked with values Python gave you: numbers, strings, lists, dictionaries. This phase teaches you to make your own kinds of values. A class describes a new kind of thing (a dog, a bank account, a chat message), and an object is one real example of it. You will learn how to give objects data and behaviour, how they find their attributes, and how Python decides which method runs.

Then you meet the tools professionals reach for every day: properties for checked attributes, inheritance and mixins, special methods that make your objects work with len, ==, + and for, dataclasses and enums for plain data, and abstract classes and protocols for swappable parts. Each tool comes with the reasons to use it and the signs that you should not.

The phase ends with design. When should you inherit, when should you compose, when is a function or a dataclass better than a class, and what does an object really cost in memory? These are the questions that separate code that is pleasant to change from code that fights you. The same ideas shape the AI code you will write later: messages and tool results are dataclasses, model status values are enums, and a swappable model client is a protocol.

0/12 · 0%
12 topics ~5 h 42 code blocks & diagrams
Start with the first topic
1
8.1

Classes and Objects

A class is a blueprint that describes what a kind of thing knows (its attributes) and what it can do (its methods); an object, also called an instance, is one real thing built from that blueprint. __init__ sets up each new object, and self is the name a method uses for the object it was called on.

28 min 1 diagram 4 code practice

2
8.2

Instance vs Class Attributes

Data can live on each object (instance attributes) or once on the class (class attributes). When you read obj.name, Python looks on the object first, then on its class, which explains shared defaults, the shared-list bug and why assigning through an instance never changes the class.

26 min 1 diagram 2 code practice

3
8.3

Instance, Class and Static Methods

Python has three kinds of method. An instance method works on one object (self), a class method works on the class itself (cls) and is the standard way to write alternative constructors, and a static method is a plain function that lives inside a class for organisation.

25 min 1 diagram 2 code practice

4
8.4

Properties and Encapsulation

Encapsulation means keeping an object's inner data behind a controlled front door. Python uses naming conventions (_name, __name) instead of locks, and @property lets you turn an attribute into a checked or computed value without changing how callers use it.

26 min 3 code practice

5
8.5

Inheritance and super()

Inheritance lets a new class (the subclass) start from an existing class (the parent) and add or change only what is different. super() reaches the parent's version of a method, and isinstance and issubclass ask whether something belongs to a family of classes.

26 min 1 diagram 3 code practice

6
8.6

Multiple Inheritance, Mixins and the MRO

A class can have several parents. Python decides which parent's method runs by following the method resolution order (MRO), computed by the C3 linearisation rule, and super() follows that same order. Mixins use this to add small, focused abilities to many classes.

25 min 1 diagram 2 code practice

7
8.7

Special Methods: The Python Data Model

Special methods (dunder methods) such as __repr__, __eq__, __len__, __iter__, __add__ and __call__ are hooks that Python calls for you when your object is printed, compared, measured, looped over, added or called. Implementing them makes your own classes behave like built-in types.

28 min 1 diagram 2 code practice

8
8.8

Dataclasses

A dataclass is a class for holding data. The @dataclass decorator reads the annotated fields you list and writes __init__, __repr__ and __eq__ for you, with options for defaults, immutability (frozen), ordering, slots and keyword-only fields.

28 min 1 diagram 3 code practice

9
8.9

Enums

An enum is a fixed set of named constants, such as Color.RED or FinishReason.STOP. Enums replace magic numbers and loose strings with names that are checked, listable and safe to compare, and they pair naturally with match.

28 min 1 diagram 3 code practice

10
8.10

Abstract Base Classes and Protocols

An abstract base class (ABC) is a parent that cannot be instantiated and forces subclasses to implement certain methods. A Protocol describes a required shape (method names and signatures) so any class that fits can be used, without inheriting, which is how duck typing becomes checkable.

27 min 1 diagram 3 code practice

11
8.11

Composition over Inheritance and Good Class Design

Composition builds an object out of other objects it holds (has-a), while inheritance says one class is a kind of another (is-a). Passing collaborators in through parameters, keeping each class to one job, and using a plain function or dataclass when a class is overkill produce code that is easy to test and change.

25 min 1 diagram 2 code practice

12
8.12

__slots__ and Object Memory

By default every object carries its own __dict__ for attributes, which costs memory. Declaring __slots__ stores attributes in a fixed layout instead, saving memory for millions of small objects and blocking accidental new attributes, at the price of flexibility.

25 min 3 code practice

Practise this phase

Hands-on exercises: write the code, run it, and press Check to have it tested automatically.