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

Terry Reedy tjreedy at udel.edu
Tue Mar 10 20:40:33 CET 2009


spir wrote:
> 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 lambda expression and a def statement both produce function 
objects.  The *only* difference between the objects produced by a lambda 
expression and the equivalent def statement is that the former gets the 
stock .__name__ of '<lambda>, which is less useful that a specific name 
should there be a traceback.  The important difference between a 
function-defining expression and statement is that the former can be 
used in expression context within statements and the latter cannot.

tjr




More information about the Python-ideas mailing list