为什么在lambda中无法打印?

问题:为什么在lambda中无法打印?

为什么不起作用?

lambda: print "x"

这不是一个单一的陈述,还是其他?该文档对于lambda允许的内容似乎有点稀疏…

Why doesn’t this work?

lambda: print "x"

Is this not a single statement, or is it something else? The documentation seems a little sparse on what is allowed in a lambda…


回答 0

一个lambda人的身体必须是一个单一的表情。在Python 2.x中,print是一条语句。但是,在Python 3中,print函数(而函数应用程序是表达式,因此它将在lambda中工作)。如果您使用的是最新的Python 2.x,则可以(并且应该,为了向前兼容:)使用向后打印功能:

In [1324]: from __future__ import print_function

In [1325]: f = lambda x: print(x)

In [1326]: f("HI")
HI

A lambda‘s body has to be a single expression. In Python 2.x, print is a statement. However, in Python 3, print is a function (and a function application is an expression, so it will work in a lambda). You can (and should, for forward compatibility :) use the back-ported print function if you are using the latest Python 2.x:

In [1324]: from __future__ import print_function

In [1325]: f = lambda x: print(x)

In [1326]: f("HI")
HI

回答 1

在我将其用于简单存根的​​情况下,请使用以下方法:

fn = lambda x: sys.stdout.write(str(x) + "\n")

完美地运作。

In cases where I am using this for simple stubbing out I use this:

fn = lambda x: sys.stdout.write(str(x) + "\n")

which works perfectly.


回答 2

你写的等同于

def anon():
    return print "x"

这也会导致SyntaxError,python不允许您分配要在2.xx中打印的值;在python3中,你可以说

lambda: print('hi')

这样做是可行的,因为他们将print更改为函数而不是语句。

what you’ve written is equivalent to

def anon():
    return print "x"

which also results in a SyntaxError, python doesn’t let you assign a value to print in 2.xx; in python3 you could say

lambda: print('hi')

and it would work because they’ve changed print to be a function instead of a statement.


回答 3

Lambda的主体必须是一个返回值的表达式。 print作为语句,不会返回任何东西,甚至也不返回None。同样,您不能将的结果分配给print变量:

>>> x = print "hello"
  File "<stdin>", line 1
    x = print "hello"
            ^
SyntaxError: invalid syntax

您也不能将变量赋值放在lambda中,因为赋值是语句:

>>> lambda y: (x = y)
  File "<stdin>", line 1
    lambda y: (x = y)
                 ^
SyntaxError: invalid syntax

The body of a lambda has to be an expression that returns a value. print, being a statement, doesn’t return anything, not even None. Similarly, you can’t assign the result of print to a variable:

>>> x = print "hello"
  File "<stdin>", line 1
    x = print "hello"
            ^
SyntaxError: invalid syntax

You also can’t put a variable assignment in a lambda, since assignments are statements:

>>> lambda y: (x = y)
  File "<stdin>", line 1
    lambda y: (x = y)
                 ^
SyntaxError: invalid syntax

回答 4

你可以做这样的事情。

创建一个函数以将打印语句转换为函数:

def printf(text):
   print text

并打印:

lambda: printf("Testing")

You can do something like this.

Create a function to transform print statement into a function:

def printf(text):
   print text

And print it:

lambda: printf("Testing")

回答 5

使用Python 3.x,打印可以在lambda中工作,而无需更改lambda的语义。

以特殊的方式使用,这对于调试非常方便。我发布此“最新答案”,因为这是我经常使用的实用技巧。

假设您的“非工具化” lambda为:

lambda: 4

然后,您的“工具化” lambda为:

lambda: (print (3), 4) [1]

With Python 3.x, print CAN work in a lambda, without changing the semantics of the lambda.

Used in a special way this is very handy for debugging. I post this ‘late answer’, because it’s a practical trick that I often use.

Suppose your ‘uninstrumented’ lambda is:

lambda: 4

Then your ‘instrumented’ lambda is:

lambda: (print (3), 4) [1]

回答 6

Lambda的主体必须是单个表达式print是一个声明,很遗憾,它已经退出了。

The body of a lambda has to be a single expression. print is a statement, so it’s out, unfortunately.


回答 7

在这里,您会看到问题的答案。 print它说不是在Python中表达。

Here, you see an answer for your question. print is not expression in Python, it says.