Lambda如何运作? [英] How lambdas work?

查看:67
本文介绍了Lambda如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python官方网站上的教程来学习python,并遇到了这个例子:

I'm learning python using the tutorial on the official python website and came across this example:

>>> def make_incrementor(n):
...     return lambda x: x + n
...
>>> f = make_incrementor(42)
>>> f(0)
42
>>> f(1)
43

x 是从哪里获得价值的?我不熟悉lambda的工作方式,我了解javascript的匿名函数也很不错,但这让我很困惑.有人愿意透露一些信息吗?我将不胜感激.

Where does x get it's value from? I'm not familiar with how lambda works, I understand anonymous functions just fine from javascript but this has me stumped. Anyone care to shed some light? I'd be grateful.

推荐答案

考虑一下. f 是由 make_incrementor 函数创建的对象.

Consider this. f is the object created by the make_incrementor function.

这是一个lambda,一个匿名函数".

It is a lambda, an "anonymous function".

>>> f= lambda x: x+42
>>> f(10)
52

当我们将 f 应用于值时,出现 x 的值.

The value for x showed up when we applied f to a value.

这篇关于Lambda如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆