[Tutor] a quetionon"There should be one and preferabley one obvious way to do it"

Paul Sidorsky paulsid@shaw.ca
Tue, 22 Jan 2002 01:42:29 -0700


alan.gauld@bt.com wrote:

> > List comprehensions replace a lot of map(lambda...)
> > garbage which is very unsightly.
> 
> I disagree. I find the map(lambda... ) form much more
> readable than the confusion that is comprehensions.
> Every time I see a comprehension I have to mentally
> rewrite it in a for loop form.

I consider that a big selling point of list comprehensions; that they
are a compact representation of a for loop.  I dunno, it seems like less
of a jump to translate to me.

Now I like to think I'm coming at this from a neutral point of view
because I came from languages where both lambda and list comprehensions
were completely unthinkable concepts.  However, it could be that I'm too
used to C where it's possible to do everything but cook dinner with a
for loop, so I might not mind "for" cropping up in not-so-obvious
places.

> But then I spent a lot of time reading Lisp where every
> function starts like:
> 
> (define foo
>    (lambda x ....))
> 
> Which is directly equivalet to:
> 
> foo = lambda x:....
> 
> in Python.

I guess that's part of the problem I have with lambda; it seems me to
have been added largely to appease the many Lisp converts.  I have a
great respect for Lisp, but Python Ain't Lisp.  (Sometimes I get tempted
to start a PAL club...)  Not only that, but lambda just doesn't seem
very Pythonic to me.  It has its uses but often I think explicitly
defining a function would have been more readable in the first place.

Of course, list comprehensions were esentially stolen straight out of
Haskell and they are admittedly trickier to read than an explicit for
loop (at least, at first).  So they're not really all that different
than lambda in these respects.

> To me a map looks much cleaner than a comprehension!
> (But is less flexible I think)
> 
> Maybe if comprehensions had clause delimiters somehow, like:

Heh, I have the same problem with lambda.  Its syntax seems too jumbly
to me; that's why I called it "unsightly".  (Calling lambda "garbarge"
was definitely out of line though.)  I get thrown by the way the
arguments are specified, especially if default arguments are used.  I
would probably have an easier time with it if it worked almost like a
function, maybe like this:

f = lambda((a, b=0), a+b)

More punctuation seems to make it easier for me to parse.

> (Who is almost certainly in a minority of one on this! :-)

Heh, that's true.  But we need you to keep us from totally brainwashing
the newbies that list comprehensions are the only True Way.  :-)

-- 
======================================================================
Paul Sidorsky                                          Calgary, Canada
paulsid@shaw.ca                        http://members.shaw.ca/paulsid/