Following Łukasz’s inquiry about multi-line lambda at the EuroPython 2025 Core Dev Panel (and revisiting longstanding threads like “Are better lambdas impossible?”). I would like to propose a viable solution.
The Motivation of lamdef: It’s about Type Safety, not just Multi-line
While lambda serves us well for simple expressions, it creates a gap in modern, typed codebases.
I have drafted a Pre-PEP (lamdef/pep-tbd.rst at main · note35/lamdef · GitHub) for a new soft keyword, lamdef. It introduces a typed, multi-statement anonymous function syntax that leverages the Off-side Rule to solve the parsing ambiguity. For example:
df['status'] = df.apply(lamdef(row: Series) -> str:
score = row['score']
if score > 90:
return "Premium"
if score > 50:
return "Active"
return "Inactive"
, axis=1)
See “The Readability Gap” in the Pre-PEP for more common examples.
Proof of Concept
This is not just a theoretical idea. I have a working prototype based on CPython 3.15 (https://github.com/gkirchou/cpython/tree/lamdef) that passes the parser logic.
I am looking for feedback on the grammar choices (specifically the strict indentation rules for container delimiters) and any potential edge cases I might have missed.
Thanks!