[Python-ideas] PEP proposal: unifying function/method classes
Jeroen Demeyer
J.Demeyer at UGent.be
Thu Mar 22 16:34:44 EDT 2018
Dear python-ideas,
I would like to draft a PEP to change the implementation of functions
and methods in CPython. The idea for this PEP come from a discussion on
https://bugs.python.org/issue30071
The core idea of the PEP is to reorganize classes for functions and
methods with the goal of removing the difference between
functions/methods implemented in C and functions/methods implemented in
Python.
Currently, CPython has two different function classes: the first is
Python functions, which is what you get when defining a function with
def or lambda. The second is built-in functions such as len, isinstance
or numpy.dot. These are implemented in C.
These two classes are completely independent with different
functionality. For example, built-in functions do not have __get__ and
therefore cannot be used as methods. And only Python functions support
introspection like inspect.getargspec or inspect.getsourcefile.
This is a problem for projects like Cython that want to implement
functions in C but still want to offer functionality close to Python
functions. In Cython, this was solved by inventing a new function class
called cyfunction. Unfortunately, this new function class creates
problems, for example the inspect module does not recognize such
functions as being functions (this is the bug mentioned above) and the
performance is worse (CPython has specific optimizations for calling
built-in functions).
When you look at methods, the inconsistency increases: On the one hand,
the implementation of built-in functions and bound methods is very
similar but built-in unbound methods are different. On the other hand,
Python functions and unbound methods are identical but bound methods are
different.
Do you think that it makes sense to propose a PEP for this?
Jeroen.
More information about the Python-ideas
mailing list