Inside CPython's attribute lookup
Python's attribute lookup logic seems pretty simple at a first glance: "first
look in the instance __dict__
, then look in its type".
However, the actual logic is much more complex because it needs to take into account the descriptor protocol, the difference between lookups on instances vs types, and what happens in presence of metaclasses.
Recently I implemented preliminary support for the descriptor protocol in SPy, and and because of that I investigated into CPython source code to get a better grasp on the details. This is a write up on what I found, with links to the actual C source code, to serve as a future reference.