[Python-Dev] Can/should built-in functions get __get__?

Christian Heimes lists at cheimes.de
Fri Sep 5 02:28:01 CEST 2008


Terry Reedy wrote:
> One of the nice features of 3.0 is that differences between classes 
> defined in C and Python (other than speed) are mostly erased or hidden 
> from the view of a Python programmer.
> 
> However, there are still sometimes surprising and quite visible 
> differences between 'functions' written in C and Python.  Can these be 
> better unified also?
> 
> In particular, built-in functions, in spite of of being labeled 
> 'builtin_function_or_method', are not usable as methods because they 
> lack the __get__ method needed to bind function to instance.

Python is far too late in the release cycle to introduce a drastic 
change. The issues has been discussed about half a year ago and we 
decided against changing PyCFunction.

But it was a good thing that you've raises your concern. I totally 
forgot about the new instancemethod type. My code is still in 
classobject.c but it's not exposed to Python level. IIRC the 
Pyrex/Cython guys are in need of such a feature.

Patch:

--- Python/bltinmodule.c        (revision 66222)
+++ Python/bltinmodule.c        (working copy)
@@ -2301,6 +2301,7 @@
         SETBUILTIN("frozenset",         &PyFrozenSet_Type);
         SETBUILTIN("property",          &PyProperty_Type);
         SETBUILTIN("int",               &PyLong_Type);
+       SETBUILTIN("instancemethod",    &PyInstanceMethod_Type);
         SETBUILTIN("list",              &PyList_Type);
         SETBUILTIN("map",               &PyMap_Type);
         SETBUILTIN("object",            &PyBaseObject_Type);

Result:

$ ./python
Python 3.0b3+ (py3k:66222M, Sep  5 2008, 02:24:22)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> class Example:
...     id = instancemethod(id)
...
 >>> example = Example()
 >>> example.id() == id(example)
True

Christian



More information about the Python-Dev mailing list