我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?

我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?

这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。


当前回答

我使用lambda来创建包含参数的回调。在一行中编写lambda比编写一个方法来执行相同的功能更简洁。

例如:

import imported.module

def func():
    return lambda: imported.module.method("foo", "bar")

相对于:

import imported.module

def func():
    def cb():
        return imported.module.method("foo", "bar")
    return cb

其他回答

我不能说python对lambda的具体实现,但一般来说lambda函数真的很方便。它们是函数式编程的核心技术(甚至是技术),在面向对象程序中也非常有用。对于某些类型的问题,它们是最好的解决方案,所以当然不应该忘记!

我建议你仔细阅读闭包和map函数(它链接到python文档,但它存在于几乎所有支持函数结构的语言中),看看它为什么有用。

你可以用lambda做的任何事情,都可以用命名函数或列表和生成器表达式做得更好。

因此,在大多数情况下,在任何情况下您都应该只使用其中一种(可能除了在交互式解释器中编写的草稿代码)。

函数这是一种非官僚化的创建函数的方法。

就是这样。例如,假设你有一个主要函数,需要对值平方。我们来看看传统的方法和的方法

传统的方法:

def main():
...
...
y = square(some_number)
...
return something

def square(x):
    return x**2

方式:

def main():
...
square = lambda x: x**2
y = square(some_number)
return something

看到区别了吗?

Lambda函数非常适合用于列表,比如列表推导式或映射。事实上,列表理解是一种使用lambda来表达自己的“python”方式。例:

>>>a = [1,2,3,4]
>>>[x**2 for x in a]
[1,4,9,16]

让我们看看每个语法元素的含义:

[]:“给我一个清单” X **2:“使用这个新诞生的函数” 对于a中的x: "into each element in a"

很方便吧?创建这样的函数。让我们用lambda重写它:

>>> square = lambda x: x**2
>>> [square(s) for x in a]
[1,4,9,16]

现在让我们使用map,这是同样的东西,但更语言中立。Maps有两个参数:

(i)一个功能

(ii)可迭代对象

给你一个列表,其中每个元素它是应用于可迭代对象的每个元素的函数。

使用map,我们会得到:

>>> a = [1,2,3,4]
>>> squared_list = map(lambda x: x**2, a)

如果你掌握了lambdas和映射,你将拥有以简洁的方式操作数据的强大能力。Lambda函数既不晦涩,也不影响代码的清晰性。不要把难的东西和新东西混为一谈。一旦你开始使用它们,你就会发现非常清楚。

I started reading David Mertz's book today 'Text Processing in Python.' While he has a fairly terse description of Lambda's the examples in the first chapter combined with the explanation in Appendix A made them jump off the page for me (finally) and all of a sudden I understood their value. That is not to say his explanation will work for you and I am still at the discovery stage so I will not attempt to add to these responses other than the following: I am new to Python I am new to OOP Lambdas were a struggle for me Now that I read Mertz, I think I get them and I see them as very useful as I think they allow a cleaner approach to programming.

He reproduces the Zen of Python, one line of which is Simple is better than complex. As a non-OOP programmer reading code with lambdas (and until last week list comprehensions) I have thought-This is simple?. I finally realized today that actually these features make the code much more readable, and understandable than the alternative-which is invariably a loop of some sort. I also realized that like financial statements-Python was not designed for the novice user, rather it is designed for the user that wants to get educated. I can't believe how powerful this language is. When it dawned on me (finally) the purpose and value of lambdas I wanted to rip up about 30 programs and start over putting in lambdas where appropriate.

我可以给你们一个我需要的例子。我正在制作一个图形程序,其中使用右键单击文件并为其分配三个选项之一。事实证明,在Tkinter(我在其中编写的GUI接口程序)中,当有人按下按钮时,它不能被分配给接受参数的命令。所以如果我选择了其中一个选项并希望我选择的结果是:

print 'hi there'

那没什么大不了的。但是如果我需要我的选择有一个特定的细节。例如,如果我选择选项A,它会调用一个函数,该函数接受依赖于选项A、B或C的一些参数,TKinter不支持这一点。拉姆达是唯一的选择,实际上…