MENU

关于python中lambda函数的一些体会

January 25, 2015 • 生活

晚上的时候,看了一篇博文《Yet Another Lambda Tutorial》,用另一种方式讲了讲python中的lambda函数。我觉得写的挺好,因为是英文的,所以我在这里把作者的大致意思写下来,也供自己日后查阅。

Lambda: a tool for building functions(构建函数的工具)

python中定义函数的两种方法,一种是def,一种是lambda。前者是常规的定义函数的方法,要对函数进行命名,后者不对函数进行命名,因此又叫做匿名函数

What is lambda good for?(lambda的优点)

为什么需要lambda:

  • We don’t need lambda, we could get along all right without it. But…
  •  there are certain situations where it is convenient — it makes writing code a bit easier, and the written code a bit cleaner.

这这种特殊情形就是,one-off function(一次性函数),只使用一次的函数。函数的使用,通常是处于两个目的:(a) to reduce code duplication(降低代码重复率), (b) to modularize code(模块化代码)。但是,这样就有了疑惑:

  • First of all — Why would you want a function that is called only once? That eliminates reason (a) for making a function.(为什么我们需要调用一次的函数,这就与使用函数目的的a降低代码重复率相矛盾了)
  • And the body of a lambda can contain only a single expression. That means that lambdas must be short. So that eliminates reason (b) for making a function.(lambda只包含一条表达式,一定就很短,这就与b模块化代码矛盾了)

然后作者就举了一个使用python中tkinter写GUI的例子:

  • 使用lambda的情况
frame = tk.Frame(parent)
frame.pack()
btn22 = tk.Button(frame,
        text="22", command=lambda: self.printNum(22))
btn22.pack(side=tk.LEFT)
btn44 = tk.Button(frame,
        text="44", command=lambda: self.printNum(44))
btn44.pack(side=tk.LEFT)
  • 不适用lambda,用def的情况:

def __init__(self, parent):
"""Constructor"""
frame = tk.Frame(parent)
frame.pack()

btn22 = tk.Button(frame,
text="22", command=self.buttonCmd22)
btn22.pack(side=tk.LEFT)

btn44 = tk.Button(frame,
text="44", command=self.buttonCmd44)
btn44.pack(side=tk.LEFT)

def buttonCmd22(self):
self.printNum(22)

def buttonCmd44(self):
self.printNum(44)

看看,效果和作用还是一目了然的。

Why is lambda so confusing?(lambda为什么这样让人觉得混乱)

First Lambda is confusing because: the requirement that a lambda can take only a single expression raises the question(只需要一个表达式)。那究竟什么是expression(表达式)?接下来谈了谈表达式和statement(语句)的区别。普遍认为:expression returns (or evaluates to) a value, whereas a statement does not(表达式返回一个值,语句不返回值)。作者认为expression can also be a statement(表达式也是语句)。在谈到什么情况使用lambda的时候,作者说:

  • If it doesn’t return a value, it isn’t an expression and can’t be put into a lambda.(无返回值,不是表达式时,不用lambda)
  • If you can imagine it in an assignment statement, on the right-hand side of the equals sign, it is an expression and can be put into a lambda.(赋值语句中,位于等号=的右边,是一个表达式的时候,可以使用lambda)

Second Lambda is confusing because: the specification that a lambda can take only a single expression raises the question, Why? Why only one expression? Why not multiple expressions? Why not statements?(为什么只需要一条表达式,为什么不是多条表达式和语句?)这里原因就复杂了。lambda是python后期才加入的,当时python语法和风格已经成型,因此在这种情况下,lambda也就成了a “pythonic” way(python范儿)。尽管如此,python中lambda还是略显怪异。

Third Lambda is confusing because: lambda is usually described as a tool for creating functions, but a lambda specification does not contain a return statement(虽然lambda通常被当作是创建函数的工具,但它没有返回语句)。.其实,返回语句已经包含在lambda中了。确实如此。

Fourth Lambda is confusing because: tutorials on lambda typically introduce lambda as a tool for creating anonymous functions, when in fact the most common use of lambda is for creating anonymous procedures(有关lambda的教程把它当作是创建匿名函数的工具,而实际上大多数时候lambda的使用是创建匿名过程)。然后作者又谈了过程和函数的区别。Procedures were for doing stuff, and did not return anything. Functions were for calculating and returning values. The difference between functions and procedures was even built into some programming languages.(procedure没有返回值,function有返回值,在一些编程语言中如Pascal两者是不同的)。在python中,过程可以用函数来实现,有时候,虽然是一个procedure,但看起来却是function。因此在python中,没有必要对二者进行严格的区分。

最后,虽然lambda好用,但也不意味着任何时候都适用,不可乱用和滥用。

Last Modified: December 28, 2022