How Python Works
How Python Works
Tier 3 — the mental model that turns "weird" Python behaviour into predictable behaviour. The object model (names vs objects), the iteration protocol, functions as values, exceptions, modules, and context managers. The tier where the language stops surprising you.
Suggest an editHow Python Works Underneath
Tiers 0–2 taught you to use Python. Tier 3 explains why it behaves as it does — the handful of underlying models that turn surprising behaviour into predictable behaviour. The thesis of the tier: almost every Python "gotcha" is a consequence of one of a few mechanisms — names bind to objects, iteration is a protocol, functions are objects, exceptions propagate up the stack — and once you hold those, you can derive the behaviour instead of memorising it.
Six chapters, in order:
- The Object Model — everything is an object; names are labels.
isvs==, mutability, aliasing, copying, argument passing. - Iterators, Iterables & Generators — how
forreally works, andyieldfor lazy streams. - Functions in Depth — functions as values:
*args/**kwargs, closures, decorators, the mutable-default and late-binding traps. - Errors & Exceptions — propagation,
try/except/else/finally, custom exceptions, EAFP. - Modules, Packages & Imports — organising code across files; the import-once-and-cache model.
- Files & Context Managers —
withand guaranteed cleanup via__enter__/__exit__.
Two of these are deep passes that revisit gentle Tier 0–1 topics with full rigour: The Object Model deepens Variables & Basic Types, and Functions in Depth deepens Functions, the Basics. They reference the earlier passes rather than repeating them.
📘 How to read the Intuition boxes. Each one is built in three moves:
- The mechanism — what the interpreter is actually doing.
- A concrete bite — a specific, runnable way the naive assumption fails.
- The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.