您的位置:首页 > 编程语言 > Python开发

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

2018-01-24 23:41 369 查看
看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 是返回值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: