无聊之人--除了技术,还是技术,你懂得
分类: Python/Ruby
2011-06-30 20:25:38
4.7. Using lambda Functions
Lambda函数的使用方法
Python supports an interesting syntax that lets you define one-line mini-functions on the fly. Borrowed from Lisp, these so-called lambda functions can be used anywhere a function is required.
Python支持一种有趣的语法,它可以让你定义一个只有一行紧凑的小函数。该想法借鉴Lisp,通常被称为lambda函数。它可以出现在任何函数能够出现的地方。
Example 4.20. Introducing lambda Functions
例4.20 lambda函数简介
This is a lambda function
that accomplishes the same thing as the normal function above it. Note the
abbreviated syntax here: there are no parentheses around the argument list,
and the return keyword is missing (it is implied, since the entire
function can only be one expression). Also, the function has no name, but it
can be called through the variable it is assigned to. 该式是一个lambda 表达式,它完成了一个通常使用函数所完成的同样工作。注意这个的简单记法:参数列表两边没有括号包括,同时不需要关键字return(这是lamda表达式的隐式说明,因为整个函数就是一个表达式)。同样,这个函数没有名字,可以通过赋值的变量(将lambda表达式赋值给变量)来调用, You can use a lambda function
without even assigning it to a variable. This may not be the most useful thing
in the world, but it just goes to show that a lambda is just an in-line
function. 你可以直接调用一个lamda函数而不一定非得将它赋值给一个变量。这可能不是lambda最重要的事情,但是这表明一个lambda表达式就是一个行内函数(内联函数)
To generalize, a lambda function is a function that takes any number of arguments (including optional arguments) and returns the value of a single expression. lambda functions can not contain commands, and they can not contain more than one expression. Don't try to squeeze too much into a lambda function; if you need something more complex, define a normal function instead and make it as long as you want.
为了统一说明,一个lambda函数是一个函数,该函数可以接受任何参数(包括可选参数),同时返回一个单一的表达式值。Lambda函数内不能包含命令,同时函数内不能包含多行表达式。不要尝试压缩多行代码在一个lambda内。如果你的业务逻辑很复杂,那么定义一个函数,这样在函数内你就可以定义你所需要的所有代码。
|
|
|
lambda functions are a matter of style. Using them is never required; anywhere you could use them, you could define a separate normal function and use that instead. I use them in places where I want to encapsulate specific, non-reusable code without littering my code with a lot of little one-line functions. Lambda函数是一种编程风格。使用lambda函数不是必须的,任何你可以lambda函数的地方,你都可以单独定义一个函数,从而使用函数来代替。我只有在如下的情况下才会使用lambda函数:封装特定的,不可用的代码,同时不想产生很多只有一行代码的函数。 |
|
4.7.1. Real-World lambda Functions
4.7.1 现实世界的lambda的函数
Here are the lambda functions in apihelper.py:
下面是apihelper.py中的lambda函数
Notice that this uses the simple form of the and-or trick, which is okay, because a lambda function is always true in a boolean context. (That doesn't mean that a lambda function can't return a false value. The function is always true; its return value could be anything.)
值得注意的是:这里使用了最简单的and-or 技巧,而且没有任何问题,这是因为在布尔测试中,lambda函数总是为真。(但这不意味着一个lambda函数不能返回一个假值,lambda函数总是为真,但是它的返回值可以是任何值。)
Also notice that you're using the split function with no arguments. You've already seen it used with one or two arguments, but without any arguments it splits on whitespace.
同时,看到你在使用不包含任何参数的split函数,在前面你已经看到使用一个和两个参数的例子,但是当调用split函数不传递任何参数时,默认的分隔符为空格。
Example 4.21. split With No Arguments
例4.21 不传递任何参数的split
This is a multiline string, defined by escape characters instead of triple quotes. \n is a carriage return, and \t is a tab character. 这是一个包含多行的字符串,通过使用转义字符而不是三元符,其中\n 代表回车,\t带面一个tab字符。 |
|
split without any arguments splits on whitespace. So three spaces, a carriage return, and a tab character are all the same. 不传递任何参数的split 函数使用的默认参数为空格,因此三个空格,一个回车,一个tab字符效果是一样的。 |
|
You can normalize whitespace by splitting a string with split and then rejoining it with join, using a single space as a delimiter. This is what the info function does to collapse multi-line doc strings into a single line. 你可以规范化空格:首先对一个字符串调用split函数,然后通过使用join在重新组装,使用一个只包含一个空格的分隔符。这就是info函数所做的,将多行文档化字符串整理成一行的字符串。 |
So what is the info function actually doing with these lambda functions, splits, and and-or tricks?
那么通过使用lambda函数,split,以及and-or技巧,info 函数的实际作用是什么呢?
processFunc is now a function, but which function it is depends on the value of the collapse variable. If collapse is true, processFunc(string) will collapse whitespace; otherwise,processFunc(string) will return its argument unchanged.
现在processFunc是一个函数,但是该函数的值有collpase变量的值来决定。如果collapse的值为真,函数processFunc(string)将取出多余的空格,否则 processFunc(string)将直接返回函数。
To do this in a less robust language, like Visual Basic, you would probably create a function that took a string and a collapse argument and used an if statement to decide whether to collapse the whitespace or not, then returned the appropriate value. This would be inefficient, because the function would need to handle every possible case. Every time you called it, it would need to decide whether to collapse whitespace before it could give you what you wanted. In Python, you can take that decision logic out of the function and define a lambda function that is custom-tailored to give you exactly (and only) what you want. This is more efficient, more elegant, and less prone to those nasty oh-I-thought-those-arguments-were-reversed kinds of errors.
在一个不健壮的语言中如VB,为了实现同样的功能,你可能会创建一个函数,该函数接受一个字符串参数以及一个collapse参数,同时使用if语句来判断是否需要压缩空格,然后来确定合适的返回值。这种方式效率很低,这是因为函数要处理每种可能的情况。每次你调用它,在进行返回值之前,函数都需要判断是否需要压缩空格。在Python中,你可以在函数外处理这种逻辑,通过定义一个lambda函数,该函数表达式自定义分隔符来返回你想要的结果。Python中的这种方式更加高效,更加的优雅,更加不容易产生哪些糟糕的代码(我所认为的那些参数所带来的各种错误)。