4

In python3, following works:

print(3); print(5)

However following gives a syntax error due to semicolon:

(lambda key: (print(3); print(5)))

Why is that, and is there a way to write a lambda function in single line (I intend to pass it as a short argument, without defining the function elsewhere)

8
  • that was easy... should I delete the question or mark this as an answer? Commented Apr 20, 2018 at 12:52
  • 2
    i suspect this is an XY problem but impossible to know without more context... Commented Apr 20, 2018 at 12:56
  • What are you using this for? Is this simply so that you can print items across multiple lines in one line of code? Commented Apr 20, 2018 at 12:57
  • 4
    You can just use def, and it'd be way more readable. You don't have to define it "elsewhere" per se, you can use a nested def if need be. Commented Apr 20, 2018 at 13:00
  • 1
    somewhat dirty (but maybe ok for debugging I guess) min(lst, key=lambda x: print(x) or x[1]) Commented Apr 20, 2018 at 13:01

3 Answers 3

5

Existing answers cover the "how?" of the question, but not the "why". Which is to say, why doesn't lambda: print(3); print(5) work? The answer is in the language specification.

From https://docs.python.org/3/reference/expressions.html#lambda:

Lambda expressions (sometimes called lambda forms) are used to create anonymous functions. The expression lambda arguments: expression yields a function object. [...] Note that functions created with lambda expressions cannot contain statements or annotations.

From https://docs.python.org/3/reference/simple_stmts.html?highlight=semicolon#simple-statements:

A simple statement is comprised within a single logical line. Several simple statements may occur on a single line separated by semicolons.

print(3); print(5) contains a semicolon, so it is a collection of simple statements. But a lambda can't contain statements. So a lambda can't contain print(3); print(5).


So why does (lambda key: (print(3), print(5))) work? It's because (print(3), print(5)) is not a statement. It's an expression: in particular, it is a tuple literal (formally, a parenthesized form whose expression list contains at least one comma), whose first element is a call to print with argument 3, and whose second element is a call to print with argument 5. All of this is a single expression, so lambda accepts it without trouble.

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

Comments

0

You can create a lambda function and use it in a single line. But its not with semicolons.

Define lambda in brackets and pass arguments immediately after that using brackets:

(lambda x,y: x+y)(6,4)

This will give output 10.

Comments

-1

Another possible option:

f = lambda x, y: print(x, y, sep='\n')
f(3, 4)

# 3
# 4

1 Comment

Curious, on my machine help(print) gives print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.