Prerequisites & Notation
Before You Begin
This chapter builds on Python fundamentals from Chapters 1-2. You should be comfortable with functions, closures, decorators, and the Python data model before proceeding.
- Python classes: def, init, self, instance vs class attributes(Review ch01)
Self-check: Can you define a class with init, repr, and a method that operates on instance attributes?
- Functions as first-class objects: passing functions, closures(Review ch02)
Self-check: Can you write a function factory that returns a specialized callable?
- Decorators and the wrapping pattern(Review ch02)
Self-check: Can you write a decorator that adds timing to any function?
- Python data model: dunder methods, operator overloading(Review ch01)
Self-check: Can you implement add, repr, and len on a custom class?
- Basic NumPy: arrays, indexing, broadcasting, ufuncs
Self-check: Can you explain what np.add(a, b) does and how broadcasting works?
Notation for This Chapter
Symbols and conventions introduced in this chapter. These build on the notation established in Chapters 1 and 2.
| Symbol | Meaning | Introduced |
|---|---|---|
Class definition with single inheritance from Bar | s01 | |
Decorator that auto-generates __init__, __repr__, __eq__ from class annotations | s01 | |
| Abstract base class β cannot be instantiated directly | s02 | |
| Marks a method that subclasses must implement | s02 | |
| Structural subtyping β defines an interface based on method signatures, not inheritance | s02 | |
| `__array__(self)` | Protocol method: NumPy calls this to convert an object to np.ndarray | s03 |
| `__array_ufunc__(self, ufunc, method, *inputs, **kwargs)` | Protocol method: intercepts NumPy universal function calls on custom objects | s03 |
| Method Resolution Order β the order Python searches classes for a method (C3 linearization) | s01 |