I sing the praises of lambda, my friend and savior!

Steven Bethard steven.bethard at gmail.com
Mon Oct 11 18:58:27 CEST 2004


Clark C. Evans wrote:
>    def somerandomname(args):
>        body
>    func(arg, somerandomname)
> 
> So, you've taken something that is a perfectly clear one-liner and
> converted it into a 3 line chunk of code with _zero_ added clarity,
> in fact, I'd say you've dramatically reduced clarity by requiring
> the a name be 'minted'.

The argument that was given to me is that, if I wrote my def statement as a 
general enough function, I could reuse the function at multiple points in the 
code.  This was mostly true for me, but there are some styles of code that I 
don't write too much (e.g. event-driven) so I can imagine that this might not 
be as applicable to others...

> | Well, if lambda is removed (or not , I hope an anonymous def 
> | expression is allowed, so we can write
> | 
> |    obj.f = def(args):
> |                body
> | or
> | 
> |    obj.f = def(args): body
> | 
> | or
> | 
> |    obj.f = (
> |        def(args):
> |            body
> |    ) 
> 
> Ewww.  How is this better than lambda? Let's keep lambda, no?

While I totally agree with the Ewww here, I think the point was to have full-
fledged anonymous functions, instead of lambdas, which are restricted to a 
single expression.  Of course, the syntax chosen for this is (to some degree, 
at least) arbitrary, but the idea would be to allow things like:

func(arg1, lambda arg2:
               self.update(arg2)
               return arg2*2)

My understanding is that no one's ever found an anonymous def syntax that 
everyone really likes, so I suspect the argument will continue to be "too ugly 
for too little gain in coding ease".  Especially since as soon as you really 
do need a multiline def, it only costs you one line to declare the function 
normally, and you don't get funny indentation:

def foo(arg2):
    self.update(arg2)
    return arg2*2
func(arg1, foo)

Steve




More information about the Python-list mailing list