Multi-statement lambdas
2026-11-19
Imagine you want to turn some blueberries and strawberries into jams, with log messages along the way. Here's an imperative way to do it in Python:
fruits: list[str] = ["blueberry", "strawberry"]
jams: list[str] = []
for fruit in fruits:
print("Preparing tasty " + fruit + " jam...")
jams.append(fruit + " jam")What if you wanted a functional style? You need two statements to turn fruit into jam with log messages, but Python doesn't support multi-statement lambdas [1]. The Pythonic way is to extract the would-be multi-statement lambda to a named function:
def turn_into_jam(fruit: str) -> str:
print("Preparing tasty " + fruit + " jam...")
return fruit + " jam"
fruits: list[str] = ["blueberry", "strawberry"]
jams: list[str] = list(map(turn_into_jam, fruits))An archive of the Python Wiki defends extraction to named functions because separation likely makes the function clearer, gives ample space for more statements later, and forces naming the function to stand-in as a comment.
I'm usually in favor of extraction to named functions, and Van Rossum says indentation-based blocks in expressions complicate a language's user interface, so I don't propose multi-statement lambdas be added to Python. However, I will offer reasons to prefer multi-statement lambdas anyway as a case study.
Multi-statement lambdas keep the code that processes objects near the definition of those objects. In my second example for turning fruit into jam, you need to remember what turn_into_jam does until it is used, or interrupt your top-down perusal of the code by scrolling back up. Remembering and scrolling are uncomfortable and get harder for longer code, but multi-statement lambdas can always be inlined, so need not be remembered or scrolled to.
Multi-statement lambdas also avoid unnecessary complexity the same way other inlined code does: by not introducing extra concepts. For an extracted named function, your reader is burdened by both the name and body of your function. Multi-statement lambdas prevent your reader from needing to remember the name of your function because your function doesn't have a name. This is the inverse of the ideal case in which the function body is abstracted away and your reader only needs your function's name, but if you can't avoid burdening your reader with the function body, you may as well avoid burdening them with the function name.
You might find that weird. Aren't names good? Not always. Names are good because they abstract away information and serve as self-documenting code, but that's not true for sufficiently simple functions like some multi-statement lambdas. Ousterhout says functions should be deep to hide information, but for sufficiently simple functions, there's nothing to hide. If you want to know what turn_into_jam does, you need to read the implementation [2]. You don't comment "the next statement prints something" above print calls, since you expect your reader to read the print call, and for sufficiently simple functions, instead of documenting with a name that repeats the function body, you can let your reader read the function body.
Last and least, multi-statement lambdas don't need the programmer to annotate types because type checkers like basedpyright and ty can infer them from context and don't need the def keyword. You get shorter code. I (Java programmer) don't care for shorter code achieved with syntactic sugar, but those who want to type less or save a few tokens might find this reason attractive.
[1] You could use list comprehension instead of map with a lambda, but that would deprive me of a chance to talk about lambdas, and there aren't multi-statement list comprehensions either.
[2] Longer names that explain the implementation do not help. For example, replacing the name turn_into_jam with log_statement_about_preparing_jam_and_return_jam_appended_to_string would describe the function body in enough detail to prevent your reader from needing to read the function body. But the length of the name doesn't make it easier to read than the function body, and the name reveals enough about the implementation that nothing has been abstracted away, so this doesn't reduce cognitive load.