Python官网给的tutorial中的lambda的说明
###################################3
一开始不理解为什么在f = make_incrementor(42) 之后,还可以给f传递参数即类似于f(0)或者f(1)?
后来想明白了,此处的make_incrementor函数的返回值是一个带参数的lambda函数,即lambda x: x+ n
所以f其实指代的是一个lambda函数,所以就可以对其传递参数了。
如果另
f = make_incrementor呢?
那么
f(1)的返回值是一个lambda函数,即lambda x: x + 1
######################
Lambda Expressions
Small anonymous functions can be created with the lambda
keyword. This function returns the sum of its two arguments: lambda a, b: a+b
. Lambda functions can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function definition. Like nested function definitions, lambda functions can reference variables from the containing scope:
The above example uses a lambda expression to return a function. Another use is to pass a small function as an argument: