tags:

views:

35

answers:

2

Hi

I have a number of functions with a combination of positional and keyword arguments, and I would like to bind one of their arguments to a given value (which is known only after the function definition). Is there a general way of doing that?

My first attempt was:

def f(a,b,c): print a,b,c

def _bind(f, a): return lambda b,c: f(a,b,c)

bound_f = bind(f, 1)

However for this I need to know the exact args passed to f, and cannot use a single function to bind all the functions i'm interested in (since they have different argument lists)

Thanks!

+1  A: 

You probably want the partial function from functools.

Daniel Roseman
+1  A: 
>>> from functools import partial
>>> def f(a,b,c):
...   print a,b,c
...
>>> bound_f = partial(f,1)
>>> bound_f(2,3)
1 2 3
MattH
wow thanks, very fast answers, chose MattH's since the example code makes it clearer