binding more than one attribute in a facntion

Peter Otten __peter__ at web.de
Wed Jul 26 15:52:21 EDT 2006


linuxnow at gmail.com wrote:

> I want to have a bound method that "fixes" more than one parmeter of a
> funtion. LEt me post an example.
> 
> def f(a, b, c):
>     return a + b + c
> 
> I can do:
> fplus10 = f(10)
> and then call f with 2 params and it works.
> 
> But, how can I fix 2 params:
> fplus10plus20 = f(10,20)
> ignores the second param.
> 
> fplus10plus20= fplus10(20)
> does not work either.
> 
> Can anybody show what I'm doing wrong?

Bound methods are limited to one implicit parameter. What you need is
partial function application:

>>> def f(a, b, c):
...     return a + b + c
...
>>> def partial(f, *args):
...     def g(*more):
...             return f(*args+more)
...     return g
...
>>> partial(f, 1, 2)(3)
6
>>> partial(f, 1)(2, 3)
6
>>> partial(f)(1, 2, 3)
6

See http://www.python.org/dev/peps/pep-0309/ for more.

Peter



More information about the Python-list mailing list