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.

SymbolMeaningIntroduced
β€˜classFoo(Bar):β€˜`class Foo(Bar):`Class definition with single inheritance from Bars01
β€˜@dataclassβ€˜`@dataclass`Decorator that auto-generates __init__, __repr__, __eq__ from class annotationss01
β€˜abc.ABCβ€˜`abc.ABC`Abstract base class β€” cannot be instantiated directlys02
β€˜@abstractmethodβ€˜`@abstractmethod`Marks a method that subclasses must implements02
β€˜typing.Protocolβ€˜`typing.Protocol`Structural subtyping β€” defines an interface based on method signatures, not inheritances02
`__array__(self)`Protocol method: NumPy calls this to convert an object to np.ndarrays03
`__array_ufunc__(self, ufunc, method, *inputs, **kwargs)`Protocol method: intercepts NumPy universal function calls on custom objectss03
β€˜MROβ€˜`MRO`Method Resolution Order β€” the order Python searches classes for a method (C3 linearization)s01