Function mistaken for a method

Christophe chris.cavalaria at free.fr
Thu Jun 1 15:36:48 CEST 2006


Eric Brunel a écrit :
> 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...

You have 2 ways to do it already, here's a third :

class C:
   f = None
   def __init__(self):
     if self.__class__.f is not None:
       self.x = self.__class__.f(0)
     else:
       self.x = 0



More information about the Python-list mailing list