Python-lambda是做什么用的? [英] Python - What are lambdas for?

查看:83
本文介绍了Python-lambda是做什么用的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解它们的作用以及如何使用它们,但是对于为什么将它们包含在Python中,我仍然有些困惑.与普通的函数定义样式相比,使用它们有什么好处?

I understand what they do and how to use them, but I'm still somewhat confused as to why they're included in Python. What benefit is there in using them, over the normal function definition style?

我能想到的唯一真正的区别是,您可以在表达式中创建它们.例如,如果myList是一个整数列表,并且您想向每个元素添加一个,则可以使用

The only real difference that I can think of is that you can create them inside an expression. For example, if myList was a list of ints and you wanted to add one to every element, you could use

list(map(lambda x: x+1, myList))

如果要使用函数定义来做到这一点,则必须在其他地方定义它,然后传递该变量.

Whereas if you wanted to do that with function definitions, you'd have to define it elsewhere and then pass that variable.

但是,我严重怀疑这种相对较小的便利性是否可以证明它们包含在语言中是合理的,所以我猜想我缺少了一些东西.或者,也许我是在低估能够在这样的代码行内创建函数的作用.

However, I seriously doubt that this relatively minor convenience would justify their inclusion in the language, so I'm guessing there's something I'm missing. Or, perhaps, I'm underestimating the usefulness of being able to create functions inside lines like that.

所以,这基本上是我的问题-lambda应该用于什么?为什么将它们包括在内?

So, that's basically my question - what are lambdas supposed to be used for? Why are they included?

推荐答案

对此没有深入的答案.很久以前,有人贡献了代码来实现 lambda ,并且在一个脆弱的时刻;-) Guido(van Rossum)应用了该补丁.这就是全部.

There isn't a deep answer to this. A long time ago, someone contributed the code to implement lambda, and in a weak moment ;-) Guido (van Rossum) applied the patch. That's all there is to it.

有时 很方便,尽管它大多已被过度使用.例如,在各种GUI系统中,当单击GUI中的某些元素时,您通常希望传递一个简单的回调函数来触发. lambda 真的很适合.

It is handy sometimes, although it's mostly over-used. For example, in various GUI systems you often want to pass a simple callback function to be triggered when some element in the GUI is clicked. lambdas are really nice for that.

仅供参考,这是Guido当时为Python版本1.0.0(1994年1月26日)所做的输入.您可以在Python发行版的 Misc/HISTORY 文件中找到此文件:

FYI, here's the entry Guido made at the time, for Python release 1.0.0 (26 January 1994). You can find this in a Python distribution's Misc/HISTORY file:

有一个新的关键字"lambda".形式的表达

There is a new keyword 'lambda'. An expression of the form

lambda参数:表达式

lambda parameters : expression

产生一个匿名函数.这实际上只是语法糖.您也可以使用

yields an anonymous function. This is really only syntactic sugar; you can just as well define a local function using

def some_temporary_name(参数):返回表达式

def some_temporary_name(parameters): return expression

Lambda表达式与map()结合使用时特别有用,filter()和reduce(),如下所述.感谢Amrit Prem为提交此代码(以及map(),filter(),reduce()和xrange())!

Lambda expressions are particularly useful in combination with map(), filter() and reduce(), described below. Thanks to Amrit Prem for submitting this code (as well as map(), filter(), reduce() and xrange())!

因此,怪罪Amrit Prem-大声笑;-)

So blame Amrit Prem - LOL ;-)

编辑单击此处阅读Guido关于该主题的博客文章.奇怪的是他不记得浏览 Misc/HISTORY ,忘记了补丁作者的名字,而他的记忆已经消失了好几年.好东西我仍然在为他掩饰;-)

EDIT And click here to read Guido's blog post on the subject. It's curious that he didn't remember to look in Misc/HISTORY, forgot the name of the patch author, and that his memories are off by several years. Good thing I'm still around to cover for him ;-)

这篇关于Python-lambda是做什么用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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