[Python-ideas] Ruby-style Blocks in Python Idea

spir denis.spir at free.fr
Tue Mar 10 09:19:36 CET 2009

Le Mon, 09 Mar 2009 18:04:32 -0400,
Terry Reedy <tjreedy at udel.edu> s'exprima ainsi:

> Lambdas are function-defining expressions used *within* statements that 
> give the resulting function object a stock .__name__ of '<lambda>'.  The 
> syntax could have been augmented to include a real name, so the 
> stock-name anonymity is a side-effect of the chosen syntax.
> Possibilities include
>    lambda name(args): expression
>    lambda <name> args: expression
> The latter, assuming it is LL(1) parse-able, would even be compatible 
> with existing code and could still be added.
> Contrarywise, function-defining def statements could have been allowed 
> to omit the name.  To be useful, the object (with a .__name__ such as 
> '<def>', would have to get a default namespace binding such as to '_', 
> even in batch mode.

I do not agree with that. It is missing the point of lambdas. Lambdas are snippets of code equivalent to expressions to be used in place. Lambdas are *not* called, need not beeing callable, rather they are *used* by higher order functions like map. The fact that they do not have any name in syntax thus properly matches their semantic "anonymousity" ;-)

> > A multi-line lambda (technically, multi-statement)  
> The problem is that 'multi-statement expression' is an oxymoron in 
> Pythonland.
> > would also be an anonymous function.  
> Not necessarily, and irrelevant to the essence of lambda expressions, 
> which is that they are expressions that can be used within statements.

I do not see any contradiction with the "essence of lambda expressions" here. We could have a syntax for multi-statement lambdas without any semantic contradiction. The issue is more probably that it does not fit well python's style and syntax (esp. indent) and would hinder legibility and simplicity.

print map(lambda x:
        {fact = None if x<0 else factorial(x); return "%s: %s" %(x,fact)},

It's ugly, sure.
Still, I do not see the "essence of lambda expressions" twisted.
I vote -1 to "block-lambdas" for the sake of clarity only.

la vita e estrany

More information about the Python-ideas mailing list