记录学习python时的问题(一)

看python官方文档时看到

lambda expression

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:

>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43
这个例子看不懂,故在解释器中运行了 make_incrementor(42)  结果发现这是个函数,于是恍然大悟,原来

return lambda x: x + n
返回一个匿名函数,x是传入的参数,x + n 是返回值

你可能感兴趣的:(python)