O05: what a dot does - #135
Merged
Merged
Conversation
The fifth lesson of the object model part. O04 built the list of classes a name is looked up in, this one is about the function that reads that list and the three other places it checks before and after. PyObject_GenericGetAttr does four things in a fixed order, and the whole of the precedence rule people memorise is the position of one if statement. The lesson puts the same name on the type and in the instance dict three different ways and shows which one wins each time. Also here: the two attribute hooks and why they are not variants of each other, the separate function a class lookup goes through, where the did you mean suggestion is really computed, the version tag that makes the cache work by going stale rather than being swept, and the four different specialised instructions the same line of Python turns into. Fourteen citations against v3.15.0rc1, six diagrams. One cell differs between 3.14 and 3.15, the suggestion wording, and it says so. One glossary term: data descriptor. GLOSSARY.md is now 116.
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 fifth lesson of the object model part. O04 built the list of classes a name gets looked up in. This one is about the function that reads that list, and about the three other places it looks before and after.
The point of the lesson is that four separate rules people memorise about attributes are all the same hundred and twenty line function, read in order.
What is in it:
_PyObject_GenericGetAttrWithDictdoes four things: walk the type's MRO, use what it found if that is a data descriptor, otherwise use the instance dict, otherwise fall back on what the MRO found and raise if there was nothingobj.method = somethingshadows a method for one object whileobj.some_property = somethingcalls a setter instead__getattribute__is the slot and runs for everything including__dict__, and the lesson shows an object you can no longer inspect through ordinary syntax because of it__getattr__is not a slot at all, has no default, and gets a different dispatcher installed which removes itself from the slot when there is nothing to dispatch to_Py_type_getattro_stackrefinstead, checks the metaclass MRO first, and calls any descriptor withNonewhere the instance would be, which is the difference betweenHolder.watchedandHolder().watchedAttributeErrormessages are different because they come from different functions, and the did you mean suggestion is not in the message at all: it is computed from theobjattribute when a traceback is formattedtp_version_tagon a class and its subclasses is enough to make every entry stop matchingLOAD_ATTRturning intoLOAD_ATTR_INSTANCE_VALUE,LOAD_ATTR_SLOT,LOAD_ATTR_METHOD_WITH_VALUESorLOAD_ATTR_MODULE, then failing its guard when the class changes, dropping back to the general form, and settling onLOAD_ATTR_PROPERTYFourteen citations against
v3.15.0rc1, including_PyObject_GenericGetAttrWithDict,_Py_slot_tp_getattr_hook,_Py_type_getattro_stackref,_PyType_LookupStackRefAndVersion,type_modified_unlocked,MCACHE_CACHEABLE_NAME,MAX_VERSIONS_PER_CLASSandPyDescr_IsData.Six diagrams. One cell differs between 3.14 and 3.15, the wording of the did you mean suggestion, and it carries a note. Everything else prints the same on both and in the Pyodide browser probe, specialised opcodes included.
One glossary term: data descriptor. GLOSSARY.md is now 116.
Part of #23.