[Python-Dev] Let's send lambda to the shearing shed (Re: Let's just *keep* lambda)

Bengt Richter bokr at oz.net
Thu Feb 9 21:53:39 CET 2006


On 2006年2月09日 16:41:10 +1300, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
>My thought on lambda at the moment is that it's too VERBOSE.
>>If a syntax for anonymous functions is to pull its weight,
>it needs to be *very* concise. The only time I ever consider
>writing a function definition in-line is when the body is
>extremely short, otherwise it's clearer to use a def instead.
>>Given that, I do *not* have the space to waste with 6 or 7
>characters of geeky noise-word.
OTOH, it does stand out as a flag to indicate what is being done.
>>So my vote for Py3k is to either
>>1) Replace lambda args: value with
>> args -> value
>>or something equivalently concise, or
>Yet another bike shed color chip:
 !(args:expr) # <==> lambda args:expr
and
 !(args::suite) # <==> (lambda args::suite)
(where the latter lambda form requires outer enclosing parens) But either "::" form
allows full def suite, with indentation for multilines having left edge of single indent
defined by first line following the "::"-containing line, and explicit returns for values
required and top suite ending on closing outer paren)
Probable uses for the "::" form would be for short inline suite definitions
 !(x::print x) # <==> (lambda x::print x) & etc. similarly
 !(::global_counter+=1;return global_counter)
 !(::raise StopIteration)() # more honest than iter([]).next()
but the flexibility would be there for an in-context definition, e.g.,
 sorted(seq, key= !(x::
 try: return abs(x)
 except TypeError: return 0))
and closures could be spelled
 !(c0,c1:!(x:c0+c1*x))(3,5) # single use with constants is silly spelling of !(x:3+5*x)
Hm, are the latter two really better for eliminating "lambda"? Cf:
 sorted(seq, key=(lambda x::
 try:return abs(x)
 except TypeError: return 0))
and 
 (lambda c1,c2:lambda x:c0+c1*x)(3,5) # also silly with constants
I'm not sure. I think I kind of like lambda args:expr and (lambda args::suite)
but sometimes super-concise is nice ;-)
>2) Remove lambda entirely.
>-1
Regards,
Bengt Richter


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /