Function mistaken for a method

Peter Otten __peter__ at web.de
Thu Jun 1 08:51:09 EDT 2006


Eric Brunel wrote:

> On Thu, 01 Jun 2006 13:34:53 +0200, Peter Otten <__peter__ at web.de> wrote:
> 
>> Eric Brunel wrote:
>>
>>> My actual question is: why does it work in one case and not in the
>>> other?
>>> As I see it, int is just a function with one parameter, and the lambda
>>> is
>>> just another one. So why does the first work, and not the second? What
>>> 'black magic' takes place so that int is not mistaken for a method in
>>> the
>>> first case?
>> A python-coded function has a __get__ attribute, a C-function doesn't.
>> Therefore C1.f performs just the normal attribute lookup while C2.f also
>> triggers the f.__get__(C2(), C2) call via the descriptor protocol which
>> happens to return a bound method.
> 
> Thanks for your explanations, Peter. I'll have to find another way to do
> what I want...

Maybe just

class C2(C):
    f = staticmethod(lambda x: x != 0)

Peter




More information about the Python-list mailing list