
On 2019-09-10, Stefan Behnel wrote:
I find it a bit sad that state lookups in slot methods are disadvantaged compared to normal methods, given that their entire point is to provide a performance _benefit_. However, for the time being, we do not know how relevant and visible this will become in practice, so it is good that the PEP does not propose a new/real/complex solution to it for now.
While PEP 573 accomplishes the goal of providing access to the module state, I don't find it too elegant. I would like a design that reduces the differences between native Python code functions compared to ones implemented in C code.
For native Python code (i.e. implemented by bytecode), functions keep a reference to their module namespace. I.e. the __globals__ attribute of a PyFunctionObject. I think we should have a PyFunctionObject analog for functions implemented by C code. They would bind a reference to their namespace (i.e. the module/module state) when they are defined.
That would remove the need for PyType_DefiningTypeFromSlotFunc(). Once you have the function object, you have the reference to the correct module. No need to crawl around in the MRO to find it.