Is there any way a following code works?
def f1(f2, x):
return f2(x)
def f2(y, z):
return y + z
f1(f2(10, ), 20)
output 30
f2
function misses z
. So, I want f1
to pass an argument, x
, to f2
as z
.
I will appreciate any help.
Is there any way a following code works?
def f1(f2, x):
return f2(x)
def f2(y, z):
return y + z
f1(f2(10, ), 20)
output 30
f2
function misses z
. So, I want f1
to pass an argument, x
, to f2
as z
.
I will appreciate any help.
Yes, this is what functools.partial
is all about:
from functools import partial
f1(partial(f2, 10), 20)
partial
takes as input a function f
, an an arbitrary number of unnamed and named parameters. It constructs a new function where the unnamed and named parameters are filled in already.
It is somewhat equivalent to:
def partial(func, *partialargs, **partialkwargs):
def g(*args, **kwargs):
fargs = partialargs + args
fkwargs = partialkwargs.copy()
fkwargs.update(kwargs)
return func(*farg, **fkwargs)
return g
But functools
will reduce the overhead significantly compared with the above implementation.
Another option showing lambda
expressions:
f1(lambda x: f2(10,x), 20)
Partial as shown by @Willem is more suited for your specific problem, but lambda
expressions are good to know for flexibility - the expression can become much more complex then just a partial call.