References & Further Reading
References
- L. Ramalho, Fluent Python, O'Reilly Media, 2nd ed., 2022
Chapters 7-9 on closures, decorators, and design patterns with first-class functions are the definitive Python-specific treatment. The coverage of `functools.wraps`, decorator stacking, and parameterized decorators is particularly thorough.
- D. Beazley and B. K. Jones, Python Cookbook, O'Reilly Media, 3rd ed., 2020
Chapter 9 covers metaprogramming with decorators, including decorators that can work with or without arguments, class decorators, and decorators that preserve type signatures.
- C. Hattingh, Using Asyncio in Python, O'Reilly Media, 2020
Covers context managers in the async context (`async with`), which extends the patterns from this chapter to concurrent scientific computing.
- K. Smith and J. Montanaro, PEP 318 β Decorators for Functions and Methods, 2003. [Link]
The original proposal for decorator syntax in Python. The "Design Goals" and "Current Syntax" sections explain why the `@` symbol was chosen.
- G. van Rossum and N. Coghlan, PEP 343 β The 'with' Statement, 2005. [Link]
Defines the context manager protocol and the `with` statement. Section on `contextlib` utilities is particularly relevant.
- Python Software Foundation, Python 3.12 Documentation β functools, 2024. [Link]
Official documentation for `functools.wraps`, `partial`, `lru_cache`, `cache`, `reduce`, and `singledispatch`.
- Python Software Foundation, Python 3.12 Documentation β contextlib, 2024. [Link]
Official documentation for `contextmanager`, `suppress`, `ExitStack`, `redirect_stdout`, and `nullcontext`.
Further Reading
Advanced decorator patterns
G. van Rossum, 'Five-Minute Multimethods in Python' (blog post, 2005)
Demonstrates how decorators can implement multiple dispatch (function overloading based on argument types). This idea evolved into `functools.singledispatch` in Python 3.4.
Context managers in scientific libraries
PyTorch documentation β torch.no_grad, torch.cuda.amp.autocast
Shows how production ML frameworks use context managers for gradient control and mixed-precision training. These are the patterns you will use when scaling to GPU computing in Part 3.
Functional programming in Python
M. Mertz, *Functional Python Programming*, Packt, 3rd ed., 2022
Comprehensive coverage of functional patterns in Python: higher-order functions, closures, immutability, and lazy evaluation. Goes deeper than this chapter on reduce, partial application, and function composition.
Property-based testing for decorated functions
Hypothesis library documentation (https://hypothesis.readthedocs.io/)
When writing decorators that modify function behavior, property-based testing can verify that invariants are preserved. Hypothesis generates random inputs to find edge cases your unit tests miss.