replace mothod for only one object but not for a class

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Wed Oct 15 04:41:49 EDT 2008


George Sakkis a écrit :
(snip)
> You're right of course; that's what you get with minimal testing ;)
> Still it works with a small modification, binding self to b as default
> argument:
> 
> b.foo = lambda self=b: "modified called on %s" % self

Ok, now with a real use case : use a named function instead of a lambda, 
  and define the function before having access to b !-)

> The functools.partial() solution shown before is the generalization of
> this for more than one arguments. So I'm wondering, except for the
> callable's exact type (bound method vs function vs functools.partial
> object)  is there any other difference and a reason to prefer one over
> the other ?

Consistency ? Possibly performances (NB : not benchmarked - might be 
worth a little homework to check this out) ?



> If not, functools.partial() seems more readable than
> calling explicitly the descriptor.

Perhaps is it just me starting to getting senile and resistant to new 
ideas, but while it does indeed work, I see the partial() solution as a 
WTF. Invoking function.__get__ *is* the obvious[1] way to get a bound 
method from a function.

[1] for anyone that know how Python turns functions into methods, that is...

But indeed, methods are specialized partial applications, and your 
solution is perfectly valid.



More information about the Python-list mailing list