Why doesn't print work in a lambda?
Solution 1
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
Solution 2
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.
Solution 3
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.
Solution 4
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
Solution 5
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")
Related videos on Youtube
Anycorn
Andrey Asadchev in real life. Currently working in Silicon Valley. Previously was a PostDoc at VT (MPQC project), before that PhD at Iowa State. My interests are FISHING!!!, C++, Python, numerical algorithms, parallel and scientific programming, code optimization.
Updated on April 08, 2022Comments
-
Anycorn about 2 years
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...
-
user1066101 almost 14 yearsdocs.python.org/reference/expressions.html#lambda. It says "expression", which is a link to a complete definition of all possible expressions. How is this "sparse"? What was incorrect or incomplete?
-
Anycorn almost 14 years@Lott I had misunderstanding of what expression/statement is and where print belongs. it makes sense now
-
-
Anycorn almost 14 yearsthank you, I was not sure about definition of expression versus statement, now it makes sense
-
tzaman almost 14 yearsThere's also
from __future__ import print_function
, which enables this in py2.x -
fmark almost 14 yearsOr alternatively
lambda: sys.stdout.write('hi')
-
Stephen almost 14 yearsIncomplete answer, but nice link.
-
Fred Nurk almost 13 years@fmark: Except it's not that simple in 2.x: you need to handle sys.stdout.softspace and (at least) write a newline afterwards.
-
Thomas Dignan almost 12 yearsNow I see why it was such a big deal to make it a function. Wanted to use print as a default kwarg and this fixed it. Thanks.
-
Danny Staple about 9 yearsAs an additional note - use the from future above. Use this only where that isn't available - which would be a seriously out of date version right now.
-
Ben about 6 yearsMay I know why would
from __future__ import print_function
must be at the beginning of the code? thx -
Sk. Irfan about 6 yearsWhere would I see the prints of what we have written here?
-
ivanleoncz over 5 yearsI agree with Ben's comment: I don't get this import. Python (2 or 3) has
print()
as built-in method. -
IvyMike over 5 yearsMore flexibly:
def printf(fmt, *args): print(fmt % args)