Special keyword argument lambda syntax
Nick Craig-Wood
nick at craig-wood.com
Sat Mar 14 11:30:04 EDT 2009
Beni Cherniavsky <beni.cherniavsky at gmail.com> wrote:
> This proposal outrageously suggests a special syntax for in-line
> functions passed as keyword arguments::
>
> >>> sorted(range(9), key(n)=n%3)
> [0, 3, 6, 1, 4, 7, 2, 5, 8]
>
> The claim is that such specialization hits a syntax sweet spot, and
> that this use case is frequent enough to contemplate eventually making
> it the only in-line function syntax.
-1 from me.
I think that lambda / inline functions should be discouraged as it
moves python away from, "there should be one-- and preferably only one
--obvious way to do it." IMHO Guido was right in his original impulse
to kill this second class way of making functions...
I would write the above as
def compute_key(n):
"Compute the sort key so that x, y and z are true"
return n % 3
sorted(range(9), key=compute_key)
Which I think is clearer and more obvious. It gives you the
opportunity for a docstring also.
Yes it is a bit more typing, but who wants to play "code golf" all
day?
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list