简体   繁体   English

Lambda和Python中的多个语句

[英]Lambda and multiple statements in Python

Can anyone explaine that behavior of lambda functions? 任何人都可以解释lambda的行为吗?

import sys
X = lambda: sys.stdout.write('first');sys.stdout.write("second")
X()

Returns: -> secondfirst 返回: - > secondfirst

And one more problem: 还有一个问题:

lambda: sys.stdout.write("...");sys.exit(0) 

Or 要么

lambda: sys.exit(0);sys.stdout.write("...")

Doesn't execute correctly. 无法正确执行。 And one more question, why in first primer execution flow goes from right to left? 还有一个问题,为什么在第一个引物执行流程从右到左?

Trying with: Python3+(3.4, 3.2) OS: Linux (Ubuntu), OSX 试用:Python3 +(3.4,3.2)操作系统:Linux(Ubuntu),OSX

sys.stdout.write("second") is not part of the lambda. sys.stdout.write(“second”)不是lambda的一部分。

'second' is always printed even if you don't call X. In other words calling X only prints 'first'. 即使你不打电话给X,也总是打印'秒'。换句话说,调用X只打印'first'。

Your code can be rewritten as; 您的代码可以重写为;

import sys
X = lambda: sys.stdout.write('first')
sys.stdout.write("second")
print X()

If you want two statements executed by the lambda place them in a tuple; 如果你想要lambda执行的两个语句将它们放在一个元组中;

lambda: (sys.stdout.write('first'),sys.stdout.write("second"))

There are two statements on on line, first is in a lambda that only gets called after the sys.stdout.write("second") has already run. 在线有两个语句,第一个是在一个lambda ,只有在sys.stdout.write("second")已经运行后才被called X() calls the lambda. X()调用lambda。

So it does not go right to left, we just have a lambda that only gets called on the next line. 所以它不是从右到左,我们只有一个lambda只能在下一行被调用。 It is no different to defining a function, writing to sys.stdout and then calling that function. 定义一个函数,写入sys.stdout然后调用该函数也没有什么不同。

Doing the equivalent with a normal function: 使用正常功能执行等效操作:

sys.stdout.write("second") 
def x():
    sys.stdout.write('first')

x()    

For your other example you need to assign the lambda and then call it: 对于您的其他示例,您需要分配lambda然后调用它:

x= lambda: sys.exit(0);sys.stdout.write("...")
x()

Using a ; 使用; and having multiple statements on one line is not pythonic or a very good idea generally. 并且在一行上有多个语句通常不是pythonic或非常好的主意。

The syntax for lambda is: lambda的语法是:

lambda <args>: <expression>

where <expression> must be a single expression. 其中<expression>必须是单个表达式。 It cannot be a statement, or multiple statements, or multiple expressions separated by ; 它不能是一个语句,或多个语句,或多个表达式分隔; .

What happens in your code is that lambda has higher priority over ; 你的代码中发生的事情是lambda具有更高的优先级; , so that gets parsed as: X = lambda: sys.stdout.write('first') followed by sys.stdout.write("second") . ,因此它被解析为: X = lambda: sys.stdout.write('first')后跟sys.stdout.write("second") Adding parentheses around sys.stdout.write('first') ; sys.stdout.write("second") sys.stdout.write('first') ; sys.stdout.write("second")周围添加括号sys.stdout.write('first') ; sys.stdout.write("second") sys.stdout.write('first') ; sys.stdout.write("second") would not work and produce a syntax error. sys.stdout.write('first') ; sys.stdout.write("second")不起作用并产生语法错误。

My trick to do multiple things inside a lambda is: 我在lambda中做多个事情的诀窍是:

f = lambda: [None, sys.stdout.write('first'), sys.stdout.write("second")][0]

and the other one: 另一个:

f = lambda: [None, sys.stdout.write("..."), sys.exit(0)][0]

However that kind of defeats the purpose of a lambda function, which is to do something short and really simple. 然而,这种做法违背了lambda函数的目的,即做一些简短而简单的事情。

I guess that would still be OK in your specific example, but kind of looks like a hack. 我想在你的具体例子中仍然可以,但有点看起来像黑客。

First code translates to: 第一个代码转换为:

import sys
X = lambda: sys.stdout.write('first')
sys.stdout.write("second")
X()

As you can see, it's now clear second runs before first . 正如你所看到的,它现在是明确的second之前运行first

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM