[Python-ideas] Multi Statement Lambdas

Vladimir Filipović hemflit at gmail.com
Sun Oct 28 21:21:35 EDT 2018


On Tue, Oct 23, 2018 at 1:16 PM Chris Angelico <rosuav at gmail.com> wrote:

> a lambda function should be treated as a block of code
> inside another function, and not as its own entity. You don't give a
> name to the block of code between "if" and "else" ...

Very well put! Thanks.

> Currently, lambda functions are permitted
> to mutate objects in surrounding scopes, but not to rebind them. Well,
> actually, PEP 572 might well be the solution there, but then you
> reopen all that lovely controversy...

I don't think I see how PEP 572 could help solve even a part of it. I
mean, I get it enables expressions in general to rebind names in the
surrounding scope, but it seems (I'm re-reading the scoping section
right now) that lambdas are in effect excluded from that; that any
`x := 1` inside a lambda necessarily refers to a local `x`.

> I mentioned abusing class syntax as an alternative solution. This
> would require a new API, but it wouldn't be hard to write a wrapper.
> ...
> In some contexts, that would be every bit as good as a lambda-based
> solution. To compete, the proposed lambda syntax would have to be
> better than all these - by no means impossible, but it's a target to
> aim at.

Okay, that's... indeed looking abusive :)

In situations where it's applicable, it's arguably no worse than a
statement lambda for points 2-5 (placement, code locality, naming,
natural code inside it).

But in terms of "pomp and ceremony" (point 1) it's a bit of a step
backwards compared to even writing plain named functions: it calls for
multiple keywords and compulsory newlines for something very simple
that shouldn't visually command that much attention.

Minor point, but also understanding this construct (when reading code)
needs a bit of a learning curve, and statement lambdas wouldn't.

And it's only applicable with keyword arguments.

I understand you expect real examples. `Thread` and
`websocket.WebSocketApp` with their keyword arguments were genuine
examples from my experience, whereas I couldn't say I've ever felt a
need to pass a statement-lambda to `map` or `reduce`. So maybe that
disqualifies the positional-arguments objection against this solution,
but the verbosity complaint stands.


More information about the Python-ideas mailing list