Special keyword argument lambda syntax

MRAB google at mrabarnett.plus.com
Fri Mar 13 18:32:45 CET 2009


Hrvoje Niksic wrote:
> MRAB <google at mrabarnett.plus.com> writes:
> 
>>>>> sorted(range(9), def key(n): n % 3)
>> [0, 3, 6, 1, 4, 7, 2, 5, 8]
> 
> Given the recent pattern of syntactic constructs for expressions using
> <expr> <keyword> <expr> (ternary if, listcomps, genexps), and avoiding
> the use of colon in expressions, maybe it should be:
> 
> sorted(range(9), key=n % 3 def key(n))
> 
> this is analogous to
> 
> sorted(range(9), foo=n % 3 if bla(n))
> 
> It also shares the property that it reuses an existing keyword and
> avoids the word "lambda", originally (I presume) an homage to Church,
> now considered obscure-sounding by many.
> 
If you want to avoid the colon then:

 >>> sorted(range(9), def key(n)=n % 3)
[0, 3, 6, 1, 4, 7, 2, 5, 8]

which would be syntactic sugar for:

 >>> sorted(range(9), key=lambda n: n % 3)



More information about the Python-list mailing list