3

Can someone explain to my why this very simple lambda expression does not work and what exactly is wrong?

 In [8]: print_me = lambda x: print x
  File "<ipython-input-8-269dab9ac1a1>", line 1
    print_me = lambda x: print x
                             ^
SyntaxError: invalid syntax

but

print_me = lambda x: x 

does ?

0

1 Answer 1

4

In python 2.X print is a statement not a function. Accordingly, this is not supported by lambda functions. In python 3, however, lambda x: print(x) works since print is a function. You could also try to import the new print functionality in python 2.X:

from __future__ import print_function 
f = lambda x: print(x)
f(4)

which prints 4.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.