O04: the order things are found in - #134
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
D().where()gives youCrather thanA, straight out of the flat__mro__tuple__mro__exactly for eleven classes includingbool,io.StringIO,collections.OrderedDictandtypesuperreading the MRO of the instance rather than of the class the call was written in, so the same unchanged method inLeftreturnsBasefor one instance and dispatches toRightfor another__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 ownmro(), which can hand back an order C3 would never produceThirteen citations against
v3.15.0rc1:pmerge,tail_contains,check_duplicates,set_mro_error,mro_implementation_unlockedand its fast path,mro_invoke,mro_hierarchy_for_complete_type,type_mro_modified,_PySuper_LookupDescrandfind_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.