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 edit

How 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:

  1. The Object Model — everything is an object; names are labels. is vs ==, mutability, aliasing, copying, argument passing.
  2. Iterators, Iterables & Generators — how for really works, and yield for lazy streams.
  3. Functions in Depth — functions as values: *args/**kwargs, closures, decorators, the mutable-default and late-binding traps.
  4. Errors & Exceptions — propagation, try/except/else/finally, custom exceptions, EAFP.
  5. Modules, Packages & Imports — organising code across files; the import-once-and-cache model.
  6. Files & Context Managerswith and 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:

  1. The mechanism — what the interpreter is actually doing.
  2. A concrete bite — a specific, runnable way the naive assumption fails.
  3. The earned rule — the decision heuristic, now justified rather than asserted, plus its cost.
Mark as read