All,
def a(p):
return p+1
def b(func, p):
return func(p)
b(a,10) # 11
here I do not want the result "11" actually, what I want is a function object with the parameter has been binded, let's name it c.
when I use c()
or something alike, it will give me the result 11, possible?
Thanks!
The functools
module provides the partial
function which can give you curried functions:
import functools
def a(p):
return p+1
def b(func, p):
return functools.partial(func, p)
c = b(a,10)
print c() # >> 11
It can be used to apply some parameters to functions, and leave the rest to be supplied:
def add(a,b):
return a+b
add2 = functools.partial(add, 2)
print add2(10) # >> 12
you can also use the functools module
import functools
def add(a,b):
return a+b
>> add(4,6)
10
>> plus7=functools.partial(add,7)
>>plus7(9)
16
The only way to do that, is to wrap it in a lambda:
c = lambda : b(a,10)
c() # 11
Though if you're going to name it anyway, that doesn't really buy you anything compared to
def c():
b(a,10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With