[Python-ideas] Syntax for passing lambdas to functions

Chris Angelico rosuav at gmail.com
Thu Feb 27 08:54:55 CET 2014


On Thu, Feb 27, 2014 at 6:32 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Chris Angelico wrote:
>>
>> It's
>> problematic because it's still legal - if it threw a SyntaxError, it
>> would at least be visible, but it doesn't:
>>
>>     spam=fire_the_ducks(42)
>>     f(onclick()=spam)
>
>
> That's equivalent to
>
>    spam = fire_the_ducks(42)
>    f(onclick = lambda: spam)
>
> which is not a syntax error either, but it's just as wrong,
> and I'm not convinced that it's a harder mistake to make.

But one of them is breaking an expression out and giving it a name,
and the other is not. Currently, in this expression:

f(arg=g(h))

you can replace f, g, and h with any expressions that result in those
same values, and the function call will be exactly the same. But
breaking out the expression part of a parenthesized call is suddenly
very different. Yes, the same can be seen with lambda, but there it's
a colon-based subexpression, and those are very obviously different
(like how an if/for/except statement behaves differently from a simple
block of code).

ChrisA


More information about the Python-ideas mailing list