The Canon

The Python Canon

Twenty-three chapters on why the language is built the way it is. Not a reference and not a list of gotchas: nine decisions made long ago and everything that follows from them — so that answers can be derived instead of remembered.

1How to read

MapOnly the top layer of every chapter. A couple of evenings on your phone, and the structure of the language is in place.
QuestionYou arrive with a specific “why does it behave like that” and take one chapter in full.
Deep diveOne root all the way down: bytes, CPython sources, measurements on your own machine.

Each chapter has three levels. L1 is a complete answer: mechanism, reason, trade-off; it is enough to make the right decisions in code. L2 is the in-memory structure and the numbers. L3 is the exact place in the CPython sources. L1 never sends you down without a reason: when it leaves something out, it says under which conditions that starts to matter.

2Order of the first pass

The chapters are linked by derivation, so on a first read the order is not arbitrary: some consequences cannot be obtained without accepting the previous root. Below, the roots come in exactly that order, not by number.

R1 object and reference → R3 reference count → R2 attribute → R4 protocols → R5 iteration → R7 import → R6 the C boundary → R8 types → R9 compatibility

R1 comes first because nothing can be derived without it. R3 follows immediately: an object's lifetime is a direct consequence of a value being an object, and the GIL comes from the same place. R6 is late because it needs both R1 and R3. R9 is last: it is not a mechanism but the frame in which all the other decisions were made, and you can only judge it once you know them.

3Hardness scale

Non-obvious claims in the text carry a badge. Telling these three things apart is a skill of its own, and it matters more than any single fact.

🔒Language guarantee. Pinned down; lean on it freely.
🔧CPython detail. Works, but nobody promised it: on another implementation or in the next release it may be gone.
🕳Undefined. Depends on the platform, the build, or simply was never pinned down by anyone.

4Chapters

A single object model with no primitives: one rule instead of two — and a header on every number as the price.

  1. 01Name, object, referenceThe definition from which mutable defaults, aliasing, the behaviour of += and the difference between is and == all follow.ready
  2. 02Numbers and strings: there are no primitivesArbitrary precision, floor division instead of truncation, three representations of a string, interning.ready
  3. 03ContainersA list as an array of pointers, the growth law, the cost of insert(0), why a million numbers weigh 36 MB.ready

The count gives deterministic destruction — and it is also what forces the GIL: the convenient with is paid for with a global lock.

  1. 09Reference countingAn object lives while something is looking at it: hence a with that works — and hence the GIL.ready
  2. 10Cycles, the collector and memoryGenerations, the unreliable __del__, turning the collector off, and why RSS is not a measure of a leak.ready

Lookup at the moment of access is the source of all the language's expressiveness and the reason for twenty years of slow attribute access.

  1. 04obj.x, end to endA four-step procedure: descriptors, property, explicit self, __slots__, inline caches.ready
  2. 05dict and hashabilityThe compact layout, the hash contract, key order as a guarantee adopted by accident.ready
  3. 06Classes__new__ versus __init__, C3 linearization, what super() actually does.ready
  4. 07Metaclassestype as a class factory — and why application code almost always gets by with __init_subclass__.ready
  5. 08Functions, closures, decoratorsCells and late binding, losing the signature without wraps, lru_cache on a method as a quiet leak.ready

An interface is not a declaration but a set of dunders; checkable contracts were bolted onto the language twice, both times much later.

  1. 11ProtocolsDunders as type slots, why len() is a function, ABCs in 2007 and Protocol in 2017.ready
  2. 12Context managers and exceptionsThe right of __exit__ to suppress an exception, return in finally, two chains of causes.ready

One protocol out of which generators grew — and out of generators, all of async.

  1. 13Iterator, generator, yield fromThe single-use iterator, the suspended frame, and why a loop does not replace yield from.ready
  2. 14async/await from the insideA coroutine as a frame, the event loop, a single synchronous call that stalls the whole service, TaskGroup versus gather.ready

A module is executed once and lives for the whole process: hence circular imports and half of the oddities in tests.

  1. 17Import, end to endExecution instead of inclusion, shared module state, where to patch a mock, circular imports.ready

The boundary the whole scientific stack stands on — and the same one that keeps the GIL from simply being removed.

  1. 15The boundary and zero-copyThe buffer protocol, memoryview, why numpy parallelizes with threads and a loop does not.ready
  2. 16Packages and rewritingABI and manylinux, why a package won't install, the full price of moving code into C or Rust.ready

Types are optional as a matter of principle: there are no proofs, but migrating a huge codebase becomes possible.

  1. 18Types are erasedAnnotations as data, TYPE_CHECKING, why a separate validation layer is needed.ready

After Python 3, breaking things is off the table. An implementation detail becomes a promise exactly when too much code has come to lean on it.

There are no separate chapters: this is a frame, not a mechanism. It lives as an essay on the root's page.

—

Cross-cutting

Five topics, each resting on several roots at once and therefore coming after them. The last two are the whole pipeline: you can read them first, as a map of the machine, or last, as everything else brought together.

  1. 19The GILWhat it actually protects, atomicity versus correctness, what free-threading changes.ready
  2. 20Concurrency: a decision treeThreads, asyncio, processes, subinterpreters — by the nature of the load and the volume of data passed.ready
  3. 21PerformanceThe order of levers from the algorithm to a rewrite; why half of the advice went stale after 3.11.ready
  4. 22From text to bytecodeNine phases from python script.py to a code object: interpreter startup, tokenizer, parser, symbol table, compiler, .pyc.ready
  5. 23The execution machineThe frame, the value stack, the evaluation loop, inline caches inside the bytecode, zero-cost exceptions, process shutdown.ready

5Summary pages

Three pages on top of the chapters. They make sense after the first pass: taken one by one they look like references, but they work as a check that the roots have come together.

SynthesisThe nine roots in one diagram, with edges of mutual influence. The GIL has no root of its own — it sits on an edge. Guarantee or detailA registry of 57 claims on the 🔒 · 🔧 · 🕳 scale. What you can lean on architecturally, and what you should not pin down even with a test. What to read nextPrimary sources, four books, talks — and, separately, what is not worth reading.

6Labs

The “get a feel for it” layer: a mechanism described in words and a mechanism you have turned with your own hands settle differently. Three interactive pages; they work on a phone and load nothing from the network.

What is in the bytesA real memory dump of eight objects, laid out by header field. Hover over a field or tap it, and its bytes light up. The count and cyclesThree scenarios step by step: where the count is enough, where it is powerless in principle, and what a real leak looks like. The GIL over timeWho holds the lock, who waits and who computes without it — for three kinds of load, next to the measurements.
What this is not. Not a Python textbook and not an explanation of syntax. Not a list of gotchas: no trap gets a chapter of its own — it appears only as a consequence of a mechanism, where that mechanism is taken apart. Not an interview trainer. And not a retelling of the documentation: where the official text is better, the chapter sends you there and says whether to read it in full or selectively.

Written by · The Python Canon