[Python-ideas] Multi Statement Lambdas

Vladimir Filipović hemflit at gmail.com
Sun Oct 21 19:16:38 EDT 2018

On Mon, Oct 22, 2018 at 12:52 AM Steven D'Aprano <steve at pearwood.info> wrote:
> > def inc_counter():
> >     counter += 1
> I don't think that's a real working example.
> ...
> You need to declare counter and sum as global variables.

Good catch!
None of the examples were real, in the sense of being copied directly
from code in actual use.
They were synthesized to illustrate a point while being as brief as possible.

I'm not even really advocating for multi-statement (or statement)
lambdas BTW, I was just answering the question of when one might even
want an anonymous function that can't be expressed using the current
lambda syntax.

> Its not the naming that would make it turn kludgy, but the use of global
> variables. If you had three websockets, would you want them all to share
> the same counter?

No, the naming would turn it kludgey precisely because I'd want
different functions. If there's only one "on_open", it suggests a
natural name for itself. If there are multiple, they can either be
called "on_open1", "on_open2"... or I can keep reusing the same name
in the same scope but that would make the code less clear, not more.

> You've fallen straight into the classic eager versus late binding of
> closures Gotcha.

I did! /facepalm
Thanks. I constructed a very poor example, and it's beginning to look
like the particular point I was trying to illustrate with that doesn't
actually stand.

> If your intention was to demonstrate that multi-statement lambda would
> be a bug-magnet, you have done an excellent job :-)

I don't know why you'd say that. That was a zero-statement lambda :)

More information about the Python-ideas mailing list