我已获得此功能。它返回函数 pair
,我认为它还返回函数 f
。这是欺骗我的部分,我不知道 f(a, b)
是什么以及如何使用它。
def cons(a, b):
def pair(f):
return f(a, b)
return pair
最佳答案
为了帮助您理解正在发生的事情,请考虑以下示例:
def cons(a, b):
def pair(f):
return f(a, b)
return pair
def my_func(a, b):
return a + b
# cons returns a function that takes a function arg and calls it with args (a, b),
# in this case (1, 3). Here a, b are considered "closured" variables.
apply_func = cons(1, 3)
print apply_func(my_func) # prints 4
关于python - 函数返回函数返回函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50536875/