我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
当前回答
在Python中,lambda只是内联定义函数的一种方式,
a = lambda x: x + 1
print a(1)
和. .
def a(x): return x + 1
print a(1)
..是完全一样的。
你可以用lambda做任何常规函数做不到的事情——Python函数和其他任何东西一样都是对象,lambdas只是定义一个函数:
>>> a = lambda x: x + 1
>>> type(a)
<type 'function'>
老实说,我认为lambda关键字在python中是多余的——我从来没有需要使用它们(或者见过使用它们的地方,常规函数、列表理解或许多内置函数中的一个本可以更好地使用)。
对于一个完全随机的例子,摘自文章“Python的lambda被破坏了!”:
要查看lambda是如何被破坏的,请尝试生成一个函数fs=[f0,…,f9]其中fi(n)=i+n。第一次尝试: >>> fs = [(lambda n: I + n) for I in range(10)] > > > fs [3] (4) 13
我想说的是,即使这样确实有效,它也太可怕了,而且是“非python化的”,同样的功能可以用无数其他方式来编写,例如:
>>> n = 4
>>> [i + n for i in range(10)]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
是的,这是不一样的,但我从未见过需要在列表中生成一组lambda函数的原因。这在其他语言中可能是有意义的,但Python不是Haskell(或Lisp,或…)
请注意,我们可以使用lambda,仍然可以达到预期的效果 结果如下: >>> fs = [(lambda n,i=i: i + n) for i in range(10)] > > > fs [3] (4) 7
编辑:
在一些情况下lambda是有用的,例如在PyQt应用程序中连接信号时,它通常很方便,像这样:
w = PyQt4.QtGui.QLineEdit()
w.textChanged.connect(lambda event: dothing())
只是执行w.textChanged.connect(dothing)将使用额外的事件参数调用dothing方法并导致错误。使用lambda意味着我们可以整齐地删除参数,而不必定义包装函数。
其他回答
lambda是处理高阶函数的非常重要的抽象机制的一部分。为了正确理解它的价值,请观看Abelson和Sussman的高质量课程,并阅读《SICP》一书
这些都是与现代软件业务相关的问题,并且变得越来越流行。
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.
在Python中,lambda只是内联定义函数的一种方式,
a = lambda x: x + 1
print a(1)
和. .
def a(x): return x + 1
print a(1)
..是完全一样的。
你可以用lambda做任何常规函数做不到的事情——Python函数和其他任何东西一样都是对象,lambdas只是定义一个函数:
>>> a = lambda x: x + 1
>>> type(a)
<type 'function'>
老实说,我认为lambda关键字在python中是多余的——我从来没有需要使用它们(或者见过使用它们的地方,常规函数、列表理解或许多内置函数中的一个本可以更好地使用)。
对于一个完全随机的例子,摘自文章“Python的lambda被破坏了!”:
要查看lambda是如何被破坏的,请尝试生成一个函数fs=[f0,…,f9]其中fi(n)=i+n。第一次尝试: >>> fs = [(lambda n: I + n) for I in range(10)] > > > fs [3] (4) 13
我想说的是,即使这样确实有效,它也太可怕了,而且是“非python化的”,同样的功能可以用无数其他方式来编写,例如:
>>> n = 4
>>> [i + n for i in range(10)]
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
是的,这是不一样的,但我从未见过需要在列表中生成一组lambda函数的原因。这在其他语言中可能是有意义的,但Python不是Haskell(或Lisp,或…)
请注意,我们可以使用lambda,仍然可以达到预期的效果 结果如下: >>> fs = [(lambda n,i=i: i + n) for i in range(10)] > > > fs [3] (4) 7
编辑:
在一些情况下lambda是有用的,例如在PyQt应用程序中连接信号时,它通常很方便,像这样:
w = PyQt4.QtGui.QLineEdit()
w.textChanged.connect(lambda event: dothing())
只是执行w.textChanged.connect(dothing)将使用额外的事件参数调用dothing方法并导致错误。使用lambda意味着我们可以整齐地删除参数,而不必定义包装函数。
我使用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)
我不能说python对lambda的具体实现,但一般来说lambda函数真的很方便。它们是函数式编程的核心技术(甚至是技术),在面向对象程序中也非常有用。对于某些类型的问题,它们是最好的解决方案,所以当然不应该忘记!
我建议你仔细阅读闭包和map函数(它链接到python文档,但它存在于几乎所有支持函数结构的语言中),看看它为什么有用。