"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, which led me to investigate the CPython source code to get a better grasp on the details."
"obj.attr and C3.attr return the very same object C3.prop and C3.meth return the unmodified "property object" and "function object" obj.prop and obj.meth return something different: Python "knows" that to get obj.prop it needs to call the property getter function, while to get obj.meth it needs to create a bound method (which we can call later if we want, but that's not the point here)."
Attribute lookup in Python involves instance __dict__ and class/type lookup but is complicated by the descriptor protocol, lookup differences between instances and types, and metaclass behavior. Instances and classes normally store attributes in __dict__, though some objects lack one. Lookup on an instance checks the instance __dict__, then the class and its superclasses. Descriptors such as properties and functions alter lookup results: class attributes return raw property or function objects, while instance access invokes property getters or produces bound methods. The descriptor protocol defines how attribute access on types and instances yields different values. CPython source code contains the detailed implementation.
Read at Antocuni
Unable to calculate read time
Collection
[
|
...
]