1

In python interpreter, I can define a function to be 1 but not print(1)

>>> lambda: 1
<function <lambda> at 0x023D03F0>
>>> lambda: print(1)
  File "<stdin>", line 1
    lambda: print(1)

Why?

0

1 Answer 1

2

This happens because (at least in traditional Python 2), print is not a function, it's a statement - so it doesn't make sense in the body of a lambda.

In Python 3, or in Python 2 with the print_function option enabled, print is a function, and it does work in a lambda:

>>> from __future__ import print_function
>>> lambda: print(1)
<function <lambda> at 0x7f69ab049578>
>>>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.