175

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...

Augusta
  • 7,171
  • 5
  • 24
  • 39
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • 1
    http://docs.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? – S.Lott Jun 04 '10 at 02:49
  • 3
    @Lott I had misunderstanding of what expression/statement is and where print belongs. it makes sense now – Anycorn Jun 04 '10 at 05:31

10 Answers10

202

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
28

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.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
Danny Staple
  • 7,101
  • 4
  • 43
  • 56
  • 3
    As 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. – Danny Staple Apr 04 '15 at 20:53
25

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.

dagoof
  • 1,137
  • 11
  • 14
11

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")
Victor Martins
  • 739
  • 7
  • 21
11

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
Paul Kuliniewicz
  • 2,711
  • 18
  • 24
5

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]
Jacques de Hooge
  • 6,750
  • 2
  • 28
  • 45
3

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

tzaman
  • 46,925
  • 11
  • 90
  • 115
2

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

vpit3833
  • 7,817
  • 2
  • 25
  • 25
2

in python3 print is a function, and you can print and return something as Jacques de Hooge suggests, but i like other approach: lambda x: print("Message") or x

print function returns nothing, so None or x code returns x other way around:
lambda x: x or print("Message") would print message only if x is false-ish

this is widely used in lua, and in python you can too instead of a if cond else b write cond and a or b

Dmitry
  • 71
  • 2
  • 2
1

If you want to print something inside a lambda func In Python 3.x you can do it as following:

my_func = lambda : print(my_message) or (any valid expression)

For example:

test = lambda x : print(x) or x**x

This works because print in Python 3.x is a function.

rkachach
  • 16,517
  • 6
  • 42
  • 66