[Python-ideas] Syntax for passing lambdas to functions

Chris Angelico rosuav at gmail.com
Wed Feb 26 01:34:10 CET 2014


On Wed, Feb 26, 2014 at 11:17 AM, Greg Ewing
<greg.ewing at canterbury.ac.nz> wrote:
> Here's a suggestion for one of them. Suppose we could
> write things like:
>
>    sorted(things, key(x) = x.date)
>
>    Button("Do it!", on_click() = fire_the_ducks())
>
> It only addresses the case of passing a function using
> a keyword argument, but I think it would make for very
> readable code in those cases. And it doesn't use any
> colons!

Gut feeling: Do not like something that puts magic into one specific
place. Apart from the * and ** unpacking tools, there's nothing
special about a function's arguments that lets you use special syntax
for creating a function, or anything else. One of Python's strengths
is that a given expression results in a given value, and that's true
regardless of where that value's going. Having these two do very
different things is confusing:

Button("Do it!", on_click() = fire_the_ducks())

Button("Do it!", fire_the_ducks())

One of them is a keyword argument (spaced in violation of PEP8,
incidentally; not sure if that adds to the confusion by making it look
like assignment when it isn't), and the other is a positional
argument. Everywhere else in Python, that would be the whole
difference. Now, apparently, one of them calls a function now and
passes its return value, and the other creates a lambda that'll call
the function later.

Oh, and also: Why create a lambda that just calls one named function?
Are you expecting the name to be rebound? Otherwise, just pass
fire_the_ducks as the on-click callback.

ChrisA


More information about the Python-ideas mailing list