Reputation: 4502
Suppose I have a function def foo(A, B)
.
Now I have another function bar(func)
, where func
is a function that takes only one variable. I want to pass in foo
as func
, but with the second variable B
always fixed to 300. How can I do that?
Upvotes: 1
Views: 74
Reputation: 7095
Lambda is easiest, but you could also use functools.partial for more complex cases:
import functools
bar(functools.partial(foo, B=300))
Upvotes: 2
Reputation: 310117
You use lambda
:
bar(lambda x: foo(x,300))
basically,
func = lambda x: x*x
is more or less equivalent to:
def func(x):
return x*x
So in this case, we use something that's more or less equivalent to:
def new_func(x):
return foo(x,300)
and then we pass the equivalent of new_func
to bar
.
Upvotes: 3