Securing a future for anonymous functions in Python

Anna annaraven at gmail.com
Mon Jan 10 06:47:05 EST 2005


Jacek Generowicz wrote:
> "Anna" <annaraven at gmail.com> writes:
>
> > With class and def, I at least have a *name* to start with - class
> > Square pretty obviously is going to have something to do with
> > geometric shapes, I would hope (or maybe with boring people...).
>
> Or maybe with multiplying something by itself. Or maybe the author
> made some changes in his program, and forgot to rename the class
> sensibly, and the class' functionality has nothing to do with squares
> of any sort any more. Or maybe he felt the name "Square" was very
> appropriate for something else in his program and inadvertently gave
> the same name to two different entities thereby clobbering the one
> whose use was intended at this point.

Idjits abound. ;-)

Can't make anything foolproof because fools are so ingenious.

> > Whereas, with lambda - I have *nothing* to go on.
>
> Aaah. OK, you object to lambda because it gives you no clue as to
what
> the function does, rather than with the word "lambda" itself? Is that
> it?
>
> So, IIUC, you consider
>
>    def add_one(x):
>        return x+1
>
>    map(add_one, seq)
>
> to be clearer than
>
>    map(lambda x:x+1, seq)

Completely, totally, and unambiguously: the version with the defined
function is immediately clear to me; the version with the lambda is
decipherable, but requires deciphering (even at 2nd and 3rd glance).
But first, wouldn't something like:

[x+1 for x in seq]

be even clearer?

Given an example more complex (which you must admit, lambdas usually
are) - the name of the function is something my brain can hold on to to
represent the group of operations; where with the lambda, I need to
mentally go through each operation each time I try to read it. And the
more complex f is, the harder time I have holding it all in my head
while I figure out how to get from the beginning value x to the ending
value f(x). lambda is an O(N*N) problem for my brain.

I could see someone more mathematically-minded being happier with
lambda. It's not, after all, the word "lambda" itself; I would still
have some issues with using, say "function", instead of "lambda", but
at least then I would immediately know what I was looking at...

Anna




More information about the Python-list mailing list