这些lambda函数返回结果13.为什么?有人可以帮我理解吗?谢谢,
two_to_one = lambda g: (lambda y: g(y, y))
one_to_two = lambda f: (lambda x, y: f(x)+f(y))
h=one_to_two(two_to_one (lambda x, y: x*y))
print (h(3, 2))
输出:
13
答案 0 :(得分:3)
细分版本:
def two_to_one(foog):
def foo1(y):
return foog(y,y)
return foo1
def one_to_two(foof):
def foo2(x,y):
return foof(x)+foof(y)
return foo2
def foo3(x,y):
return x*y
h = one_to_two(two_to_one(foo3))
print h(3,2)
如何调用函数:
Step1:
two_to_one(foo3) returns foo1. Now foog = foo3
Step2:
one_to_two(foo1) returns foo2. Now foof = foo1
Step3:
h = foo2
Step4:
h(3,2) will call foo2(3,2)
Step5:
foo2(3,2) calls:
#foof(x) foog(x,x)
foo1(3) --> foo3(3,3) --> 3*3
+ --> 13 #return this from foo2
foo1(2) --> foo3(2,2) --> 2*2
#foof(y) foog(y,y)
Step6:
print 13 #print return value of h(3,2)
答案 1 :(得分:3)
此脚本使用lambda创建匿名闭包 - 绑定到调用函数时使用的变量的函数。为了弄清楚它的作用,我们可以将所有内容都显式化,并将代码与打印件混在一起,看看发生了什么。有趣的是,所有计算都在h(3, 2)
# two_to_one = lambda g: (lambda y: g(y, y))
def two_to_one(g):
"""Return a function `func(y)` that applies input function `g`
in the formula: g(y, y)
"""
def _two_to_one_closure(y):
print("_two_to_one_closure calls: {}({})".format(g.__name__, y))
rc = g(y, y)
print("_two_to_one_closure returns", rc)
return rc
return _two_to_one_closure
# one_to_two = lambda f: (lambda x, y: f(x)+f(y))
def one_to_two(f):
"""Return a function `func(y)` that applies input function `f`
in the formula: f(x) + f(y)
"""
def _one_to_two_closure(x, y):
print("_one_to_two_closure calls: {}({}) + {}({})".format(
f.__name__,x,f.__name__,y))
rc = f(x) + f(y)
print("_one_to_two_closure returns", rc)
return rc
return _one_to_two_closure
# h=one_to_two(two_to_one (lambda x, y: x*y))
def _anon(x, y):
print("_anon", x ,y)
rc = x*y
print("_anon returns", rc)
return rc
g = two_to_one(_anon) # create a _two_to_one_closure that will
print("g", g) # call _anon when called
h = one_to_two(g) # create a _one_to_two_closure that will
print("h", h) # call `g` when called
# print (h(3, 2))
print("do the calculations on 3, 2")
i = h(3, 2) # call h, which is a _one_to_two_closure
print(i)
运行它,你得到
g <function two_to_one.<locals>._two_to_one_closure at 0x7fca5165a1e0>
h <function one_to_two.<locals>._one_to_two_closure at 0x7fca5165a268>
do the calculations on 3, 2
_one_to_two_closure calls: _two_to_one_closure(3) + _two_to_one_closure(2)
_two_to_one_closure calls: _anon(3)
_anon 3 3
_anon returns 9
_two_to_one_closure returns 9
_two_to_one_closure calls: _anon(2)
_anon 2 2
_anon returns 4
_two_to_one_closure returns 4
_one_to_two_closure returns 13
13
_two_to_one_closure 2
_anon 2 2
13