Skip to content

O04: the order things are found in - #134

Merged
tamnd merged 1 commit into
mainfrom
lesson/o04-the-order-things-are-found-in
Aug 31, 2026
Merged

O04: the order things are found in#134
tamnd merged 1 commit into
mainfrom
lesson/o04-the-order-things-are-found-in

Conversation

@tamnd

@tamnd tamnd commented Aug 31, 2026

Copy link
Copy Markdown
Owner

The fourth lesson of the object model part, and the one that finishes what O03 started. O03 explained which dictionary a name gets found in. This one explains the order those dictionaries get looked at, which is the only interesting question once a class has more than one base.

The answer is a list, computed once when the class is made and stored on the type. It is not a search, and that is the thing most people are surprised by.

What is in it:

  • the diamond, and the fact that D().where() gives you C rather than A, straight out of the flat __mro__ tuple
  • swapping the declared bases gives a different list, so the bases tuple is an input to the computation rather than a record of what you typed
  • the three constraints the list satisfies, checked on classes from the notebook and from the standard library
  • the C3 merge written out in twenty lines of Python, with an assertion that it reproduces __mro__ exactly for eleven classes including bool, io.StringIO, collections.OrderedDict and type
  • the single base fast path, which skips the merge entirely
  • the two ways a class statement fails: contradictory orders, where the error names the classes the merge got stuck between rather than the bases you wrote, and a repeated base, which is caught earlier by a different check with a different message
  • super reading the MRO of the instance rather than of the class the call was written in, so the same unchanged method in Left returns Base for one instance and dispatches to Right for another
  • and the two ways the list changes afterwards: assigning __bases__, which rebuilds it for the class and every subclass while existing instances pick up the new method at once, and a metaclass with its own mro(), which can hand back an order C3 would never produce

Thirteen citations against v3.15.0rc1: pmerge, tail_contains, check_duplicates, set_mro_error, mro_implementation_unlocked and its fast path, mro_invoke, mro_hierarchy_for_complete_type, type_mro_modified, _PySuper_LookupDescr and find_name_in_mro.

Six diagrams. Every cell prints the same thing on 3.14, on 3.15.0rc1 and in the Pyodide browser probe, so this lesson needed no version notes beyond the standard banner.

Two glossary terms: MRO and C3 linearization. GLOSSARY.md is now 115.

Part of #23.

The fourth lesson of the object model part. O03 explained which dict a name
is found in, this one explains the order those dicts get read in.

The MRO is a list, computed once when the class is made and stored on the
type. The lesson writes the C3 merge out in twenty lines of Python and
asserts it matches __mro__ exactly for eleven classes, including some from
the standard library, then shows both ways a class statement can fail
before the class exists.

The part with real consequences is super. It reads the MRO of the instance
rather than of the class the call appears in, so the same unchanged method
in Left dispatches to Right when the instance is a Both.

Thirteen citations against v3.15.0rc1, six diagrams, and every cell prints
the same thing on 3.14, on 3.15.0rc1 and in the browser probe.

Two glossary terms: MRO and C3 linearization. GLOSSARY.md is now 115.
@tamnd tamnd added kind/lesson A chapter: prose, notebook, experiments, boss fight area/objects The object model, types, slots and the builtin types labels Aug 31, 2026
@tamnd
tamnd merged commit 227f2aa into main Aug 31, 2026
16 checks passed
@tamnd
tamnd deleted the lesson/o04-the-order-things-are-found-in branch August 31, 2026 16:30
@tamnd tamnd mentioned this pull request Aug 31, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/objects The object model, types, slots and the builtin types kind/lesson A chapter: prose, notebook, experiments, boss fight

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant