[Python-ideas] Enhance definition of functions

Terry Reedy tjreedy at udel.edu
Wed Jul 31 10:15:02 CEST 2013


On 7/31/2013 1:20 AM, Steven D'Aprano wrote:
> On 31/07/13 11:41, Terry Reedy wrote:
>> On 7/30/2013 11:59 AM, Ronald Oussoren wrote:
>>
>>> "Never" is a long time. AFAIK the main reason why Python doesn't have
>>> multi-line lambda's is that nobody has proposed a suitable syntax yet
>>> (and not for lack of trying, the archives of this list and python-dev
>>> contain a lot of proposals that were found lacking).
>>
>> There is also the fact that a generic .__name__ attribute of
>> '<lambda>' is inferior to a possibly unique and meaningful name. This
>> is not just in tracebacks. Consider
>>    [<built-in function sin>, <built-in function cos>]
>> versus
>>    [<function <lambda> at 0x0000000003470B70>, <function <lambda> at
>> 0x0000000003470BF8>]
>
>
> True, but if we're going to hypothesize nice syntax for multi-line
> lambdas, it's not much harder to imagine that there's also nice syntax
> to give them a name and a doc string at the same time :-)

But then they would not be anonymous.

When I have a multiple line expression, I often pull out an anonymous 
expression and give it a local name to use within the expression to make 
the expression shorter and more comprehensible. So does most everyone 
else. So I have no desire to do the opposite with function definitions 
by sticking multiple line definition in the middle of an expression. I 
know Lisper do that, and the result is ofter difficult to impossible to 
read.

In sort(key=?) calls, I use lambda for short, one-use key expressions, 
but I would never want to inject a multiple line expression into the 
middle. To me, it make the code less readable than separating a 
non-trivial key idea from the sort idea.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list