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

Terry Reedy tjreedy at udel.edu
Fri Sep 5 08:43:17 CEST 2008



Christian Heimes wrote:
> 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.

Of course.  I should have been clear that I was asking for 3.1.

 > The issues has been discussed about half a year ago and we
> decided against changing PyCFunction.

I did't remember that.  Was it a permanent or provisional decision.

> 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

I consider that 2nd best but it will solve the problem once one is aware 
of it.  I am thinking of suggesting a new paragraph for the Built-in 
Functions sections warning of differences between built-in functions and 
function instances.

Terry



More information about the Python-Dev mailing list