replace mothod for only one object but not for a class
bruno.42.desthuilliers at websiteburo.invalid
Wed Oct 15 10:41:49 CEST 2008
George Sakkis a écrit :
> 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
> 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 way to get a bound
method from a function.
 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