GadaaLabs
Python Mastery — From Zero to AI Engineering
Lesson 5

Object-Oriented Programming — From Classes to Protocols

38 min

Part 1: Classes from Scratch

The Class Statement and __init__

A class is a blueprint for objects. When you call MyClass(), Python allocates a new instance (__new__), then calls __init__ to initialize it. self is not a keyword — it is simply the first parameter of every instance method, and Python automatically passes the instance as that first argument.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Instance vs Class Attributes

Python
Click Run to execute — Python runs in your browser via WebAssembly

classmethod and staticmethod

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 2: Dunder (Magic) Methods

__repr__ and __str__

Python
Click Run to execute — Python runs in your browser via WebAssembly

Comparison Operators and @total_ordering

Python
Click Run to execute — Python runs in your browser via WebAssembly

Arithmetic Operators and Reflected Methods

Python
Click Run to execute — Python runs in your browser via WebAssembly

Container Protocol

Python
Click Run to execute — Python runs in your browser via WebAssembly

Context Manager, __call__, __bool__, and __slots__

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 3: Inheritance and MRO

Single Inheritance and super()

Python
Click Run to execute — Python runs in your browser via WebAssembly

Method Resolution Order (MRO) — C3 Linearization

Python uses the C3 linearization algorithm to compute the MRO. The invariant: a class always appears before its parents, and if two classes share an ancestor, the one listed first in the inheritance list takes precedence. This guarantees the ancestor is called exactly once in the diamond problem.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 4: Properties and Descriptors

Properties — Controlled Attribute Access

Python
Click Run to execute — Python runs in your browser via WebAssembly

Descriptors — The Engine Behind Properties

Any class defining __get__, __set__, or __delete__ is a descriptor. Properties, classmethods, and staticmethods are all descriptors under the hood. Writing your own descriptor lets you reuse validation logic across multiple classes.

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 5: Abstract Classes and Protocols

ABC — Enforced Interfaces

Python
Click Run to execute — Python runs in your browser via WebAssembly

typing.Protocol — Structural Subtyping

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 6: Dataclasses

@dataclass — Auto-generated Boilerplate

Python
Click Run to execute — Python runs in your browser via WebAssembly

__post_init__, frozen, and inheritance

Python
Click Run to execute — Python runs in your browser via WebAssembly

Part 7: Advanced OOP Patterns

Metaclasses and __init_subclass__

Python
Click Run to execute — Python runs in your browser via WebAssembly

Observer Pattern

Python
Click Run to execute — Python runs in your browser via WebAssembly

Project: Bank Account System

Python
Click Run to execute — Python runs in your browser via WebAssembly

Exercises

Easy

1. Immutable Point3D

Python
Click Run to execute — Python runs in your browser via WebAssembly

2. StringBuilder

Python
Click Run to execute — Python runs in your browser via WebAssembly

Medium

3. Cached Property Descriptor

Python
Click Run to execute — Python runs in your browser via WebAssembly

4. Type-checked Model

Python
Click Run to execute — Python runs in your browser via WebAssembly

Hard

5. Decorator-based Event Handling

Python
Click Run to execute — Python runs in your browser via WebAssembly

6. Unit Conversion Descriptors

Python
Click Run to execute — Python runs in your browser via WebAssembly

7. Composable Pipeline

Python
Click Run to execute — Python runs in your browser via WebAssembly

8. Abstract Shape Registry

Python
Click Run to execute — Python runs in your browser via WebAssembly