我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
我在试着找出Python的lambda。lambda是那些在现实生活中应该被遗忘的“有趣”语言项之一吗?
我确信有一些边缘情况可能需要它,但是考虑到它的模糊性,它在未来版本中被重新定义的可能性(我基于它的各种定义的假设)以及降低的编码清晰度——应该避免它吗?
这让我想起了C类型的溢出(缓冲区溢出)——指向顶部变量并重载以设置其他字段值。这感觉有点像技术人员的表演,但维护程序员的噩梦。
当前回答
我发现lambda对于执行相同功能的函数列表很有用,但适用于不同的情况。
就像Mozilla的复数规则:
plural_rules = [
lambda n: 'all',
lambda n: 'singular' if n == 1 else 'plural',
lambda n: 'singular' if 0 <= n <= 1 else 'plural',
...
]
# Call plural rule #1 with argument 4 to find out which sentence form to use.
plural_rule[1](4) # returns 'plural'
如果你必须为所有这些定义一个函数,到最后你会疯掉的。 另外,像plural_rule_1, plural_rule_2这样的函数名也不太好。当你依赖于变量函数id时,你需要eval()它。
其他回答
lambdas在GUI编程中非常有用。例如,假设您正在创建一组按钮,并且希望使用单个参数化回调,而不是每个按钮使用唯一的回调。Lambda让你轻松完成:
for value in ["one","two","three"]:
b = tk.Button(label=value, command=lambda arg=value: my_callback(arg))
b.pack()
(注意:虽然这个问题是专门问lambda的,但你也可以使用functools。以获得相同类型的结果)
另一种方法是为每个按钮创建单独的回调,这可能导致重复的代码。
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不支持这一点。拉姆达是唯一的选择,实际上…
我发现lambda对于执行相同功能的函数列表很有用,但适用于不同的情况。
就像Mozilla的复数规则:
plural_rules = [
lambda n: 'all',
lambda n: 'singular' if n == 1 else 'plural',
lambda n: 'singular' if 0 <= n <= 1 else 'plural',
...
]
# Call plural rule #1 with argument 4 to find out which sentence form to use.
plural_rule[1](4) # returns 'plural'
如果你必须为所有这些定义一个函数,到最后你会疯掉的。 另外,像plural_rule_1, plural_rule_2这样的函数名也不太好。当你依赖于变量函数id时,你需要eval()它。
我是一个python初学者,所以为了获得一个清晰的lambda概念,我将它与'for'循环进行了比较;在效率方面。 下面是代码(python 2.7) -
import time
start = time.time() # Measure the time taken for execution
def first():
squares = map(lambda x: x**2, range(10))
# ^ Lambda
end = time.time()
elapsed = end - start
print elapsed + ' seconds'
return elapsed # gives 0.0 seconds
def second():
lst = []
for i in range(10):
lst.append(i**2)
# ^ a 'for' loop
end = time.time()
elapsed = end - start
print elapsed + ' seconds'
return elapsed # gives 0.0019998550415 seconds.
print abs(second() - first()) # Gives 0.0019998550415 seconds!(duh)