[Python-Dev] Are PyCFunctions supposed to invisibly consume self when used as a method?

Philip Jenvey pjenvey at underboss.org
Sat Jun 12 19:19:07 CEST 2010


On Jun 11, 2010, at 6:57 PM, Brett Cannon wrote:

> On Fri, Jun 11, 2010 at 18:30, Guido van Rossum <guido at python.org> wrote:
>> On Fri, Jun 11, 2010 at 5:41 PM, Benjamin Peterson <benjamin at python.org> wrote:
>>> 2010/6/11 Brett Cannon <brett at python.org>:
>>>> This "magical" ignoring of self seems to extend to any PyCFunction. Is
>>>> this dichotomy intentional or just a "fluke"? Maybe this is a
>>>> hold-over from before we had descriptors and staticmethod, but now
>>>> that we have these things perhaps this difference should go away.
>>> 
>>> There are several open feature requests about this. It is merely
>>> because PyCFunction does not implement __get__.
>> 
>> Yeah, but this of course is because before descriptors only Python
>> functions were special-cased as methods, and there was known code that
>> depended on this. I'm sure there's even more code that depends on this
>> today (because there is just more code, period :-).
>> 
>> Maybe we could offer a decorator that adds a __get__ to a PyCFunction though.
> 
> Well, staticmethod seems to work just as well.
> 
> I'm going to make this my first request for what to change in Py4K. =)

+1 on changing this, it's annoying for alternate implementations. They oftentimes implement functions in pure Python whereas user code might be expecting the PYCFunction behavior. 

Jython's had a couple cases of this incompatibility reported. It's a rare occurrence but it's very mysterious to the user when it happens.

--
Philip Jenvey


More information about the Python-Dev mailing list