我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
当前回答
我使用lambdas来避免代码重复。这样函数就容易理解了 例如:
def a_func()
...
if some_conditon:
...
call_some_big_func(arg1, arg2, arg3, arg4...)
else
...
call_some_big_func(arg1, arg2, arg3, arg4...)
我用一个临时变量替换它
def a_func()
...
call_big_f = lambda args_that_change: call_some_big_func(arg1, arg2, arg3, args_that_change)
if some_conditon:
...
call_big_f(argX)
else
...
call_big_f(argY)
其他回答
Lambdas实际上是非常强大的构造,它源于函数式编程的思想,而且在Python的近期内,它绝不可能被轻易地修改、重新定义或删除。它们可以帮助您编写更强大的代码,因为它允许您将函数作为参数传递,因此函数是一等公民。
Lambdas确实容易让人困惑,但一旦获得了扎实的理解,你就可以写出像这样干净优雅的代码:
squared = map(lambda x: x*x, [1, 2, 3, 4, 5])
上面的代码行返回列表中数字的平方的列表。当然,你也可以这样做:
def square(x):
return x*x
squared = map(square, [1, 2, 3, 4, 5])
显然,前一种代码更短,如果您打算只在一个地方使用map函数(或任何以函数作为参数的类似函数),则尤其如此。这也使代码更加直观和优雅。
另外,正如@David Zaslavsky在他的回答中提到的,列表推导并不总是正确的方法,尤其是当你的列表必须从一些晦涩的数学方法中获取值时。
从更实际的角度来看,lambdas最近对我来说最大的优势之一是在GUI和事件驱动编程方面。如果你看一下Tkinter中的回调,它们所接受的参数就是触发它们的事件。如。
def define_bindings(widget):
widget.bind("<Button-1>", do-something-cool)
def do-something-cool(event):
#Your code to execute on the event trigger
现在如果你有一些论点要通过呢?简单到传递2个参数来存储鼠标单击的坐标。你可以简单地这样做:
def main():
# define widgets and other imp stuff
x, y = None, None
widget.bind("<Button-1>", lambda event: do-something-cool(x, y))
def do-something-cool(event, x, y):
x = event.x
y = event.y
#Do other cool stuff
现在,您可以争辩说这可以使用全局变量来完成,但是,如果全局变量只用于一个特定的位置,您真的想要担心内存管理和泄漏吗?那只是糟糕的编程风格。
简而言之,lambdas是很棒的,永远不应该被低估。尽管Python lambdas与LISP lambdas不同(后者更强大),但您确实可以用它们做很多神奇的事情。
我怀疑lambda不会消失。 请参阅Guido关于最终放弃尝试删除它的帖子。也请参阅冲突概要。
你可以看看这篇文章,了解更多Python函数特性背后的交易历史: http://python-history.blogspot.com/2009/04/origins-of-pythons-functional-features.html
奇怪的是,最初引入lambda和其他函数特性的map、filter和reduce函数在很大程度上已经被列表推导式和生成器表达式所取代。事实上,在Python 3.0中,reduce函数已从内置函数列表中删除。(但是,没有必要投诉lambda、map或filter的删除:它们被保留了。: -)
我个人的意见是:就清晰度而言,lambda值不了多少。通常有一个更清晰的不包含的解。
Lambdas通常与函数式编程风格密切相关。通过将函数应用于某些数据并合并结果来解决问题,这是谷歌用于实现其大多数算法的思想。
以函数式编程风格编写的程序很容易并行化,因此在现代多核机器中变得越来越重要。 所以简而言之,不,你不应该忘记他们。
Lambda是一个过程构造函数。你可以在运行时合成程序,尽管Python的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.