Functions as Objects
In Python, functions are first-class. You can pass them as arguments, return them from other functions, store them in lists and dicts, and attach attributes to them. This part is about the consequences of that single fact.
19 Functions as First-Class Objects is the foundational chapter: what first-class really means, the seven kinds of callable, anonymous functions and their limits, and why
functools.partialexists.20 Type Hints in Functions turns to gradual typing — the contract Python’s type checker can read, but the interpreter happily ignores. We cover the everyday hints (
list[int],dict[str, float],Callable,Optional), and we’ll see them again in 27 More About Type Hints once protocols and generics are on the table.21 Decorators and Closures is where functions-as-objects pays the largest dividend. Decorators are the syntactic sugar; closures are the engine. We’ll build a parameterized decorator, register a class, and explore why decorator order matters.
22 Design Patterns with First-Class Functions rewrites two textbook Gang of Four patterns — Strategy and Command — without classes. The point is not that classes are bad; the point is that languages with first-class functions don’t always need them.