Hacker News new | ask | show | jobs
by gilch 1688 days ago
Where is the "no multi-line lambdas" myth coming from? I see this one a lot and its wrong. Python's lambdas can have as many lines as you want.

Did you mean to say that lambda expressions can't contain statements? But no Python expressions can! But functional programming is all done with expressions anyway, to the point where functional languages don't even have statements, so what's the problem?

1 comments

No statements means no locals, which is sometimes bad for clarity and/or performance. Functional langs without statements have let-bindings.
That's really not true either. The lambda parameters are locals [a]. A let-binding is just a lambda that you call immediately. You can use defaults so the value doesn't have to appear as an argument [b]. Also, Python has assignment expressions now [c], and comprehensions are expressions which can have their own locals [d].

    >>> (lambda a, b=2:
    ...   print(a, a, b, b, c:=3, c, *[[d,d] for d in [4]][0])
    ... )(1)
    1 1 2 2 3 3 4 4
That's four different ways to get locals with no statements.