Control Flow and Concurrency
This part is about the moments when Python’s control flow stops being plain if/for/while and starts being something else: an iterator’s __next__, a context manager’s __enter__/__exit__, a coroutine’s await, a thread’s Lock. Each one is a contract; this part is about what each contract promises.
28 Operator Overloading is the practical guide to making
+,-,*, and==work on your own classes. The trick is the reflected methods —__radd__,__rmul__— that let2 * vecwork even whenvecdoesn’t know aboutint.29 Iterators, Generators, and Classic Coroutines is the engine room. Generators,
yield from, lazy evaluation, theitertoolsmodule, and the duality between iterators and classic coroutines.30 with, match, and else Blocks covers three pieces of Python control flow that don’t appear in many other languages: context managers (
with), structural pattern matching (match), and theelseclause onfor/while/try.31 Concurrency Models in Python is the conceptual chapter. Threads vs. processes vs. coroutines; the GIL; what it means for I/O-bound vs. CPU-bound work. Read it before the next two.
32 Concurrent Executors covers
concurrent.futures— the high-level pool API that hides most of the locking and process management you’d otherwise write by hand.33 Asynchronous Programming closes the part with
asyncioitself:async def,await, the event loop, and the patterns (gather, semaphore, async for, async with) you reach for once you’re pastHello, World.