[Python-ideas] Syntax for passing lambdas to functions

Steven D'Aprano steve at pearwood.info
Thu Feb 27 05:19:58 CET 2014


On Wed, Feb 26, 2014 at 11:44:51AM -0800, Andrew Barnert wrote:

> But it becomes more useful if you do anything else:
> 
>     Button("Do it!", on_click() = fire_the_ducks(42))
> 
> 
> At first glance, I think this is nice, 

At first glance, it looks like you are setting the on_click argument to 
the result of fire_the_ducks(42). This proposed syntax is going to be 
*really easy* for people to misinterpret when they see it in use. And 
not just novices -- I think this will be syntax that just begs to be 
misinterpreted when reading code, and misused when writing it.

I think that having special syntax for anonymous function only inside 
function calls with keyword arguments is a violation of the Zen of 
Python (see the one about special cases) and the Principle Of Least 
Surprise. It's really a bad idea to have syntax for a "shorter lambda" 
that works here:

    f(arg=***whatever***)

but not in these:

    f(***whatever***)
    [len, zip, map, ***whatever***, some_function]
    result = ***whatever***(arg)


One of the best things about Python is it's internal consistency. It is 
remarkably free of special case syntax that works in one place but not 
in others. Let's keep it that way.



-- 
Steven


More information about the Python-ideas mailing list