PEP scepticism

Steve Horne sh at ttsoftware.co.uk
Mon Jul 2 09:21:38 EDT 2001


On 28 Jun 2001 13:51:18 -0400, jmcbray at carcosa.net (Jason F. McBrayer)
wrote:

>>>>>> "RS" == Roman Suzi <rnd at onego.ru> writes:
>RS> There is limit after which more explanation is worse than less:
>
>RS> processed_list = []
>RS> for i in old_list:
>RS>   processed_list.append(func(i))
>
>RS> adds nothing but noise, while:
>
>In this trivial case, probably not, but...
>
>RS> processed_list = [func(i) for i in old_list]
>
>RS> makes it clear as day!
>
>Not to me it doesn't -- it uses another piece of grammar that I've
>never seen before.  To me, it's clear as mud.

I disagree. Just because you haven't seen it before, it doesn't proove
the meaning isn't obvious. On that logic, I'd have to claim the basic
for loop is clear as mud - I never saw a for loop work like that
before Python, yet I understood it the first time I saw it.

As it happens, although I did use a couple of functional languages
once, it was at college many years ago. I'd swear that list
comprehensions did not exist in either LISP or Miranda - at least at
the level I used them. I certainly didn't know what the term meant
until after I'd read some Python docs. Yet I did understand the syntax
on the first glance. After all, it is rather reminiscent of a maths
notation that I learnt when I was about 14 years old - the only
difference being the use of English words rather than wierd symbols.

Besides, the whole point of very high level language is that you don't
have to specify all the low level details of how to implement things.
Instead, you simply specify what you want and let the interpreter do
the rest. That is exactly what list comprehensions give you.


>RS> (I prefer map(), but this is personal)
>
>I would have used map(), too.

Lets examine that...


processed_list = [i * k for i in old_list]

processed_list = map (lambda i, k=k : i * k, old_list)


The first version is more compact - admittedly only a little in this
case, but more so for real examples. It also describes what it does,
making it easier for a Python-newbie to read.

Newbies would probably be tripped up several times by the second
example. The most blatant stumbling block is the lambda - lambda is
not exactly an everyday part of peoples vocabulary, and even if
someone guessed that it was a quick function definition which could be
mysteriously passed as a parameter, they'd certainly be worried by the
'k=k'.

Of course the 'k=k' could be avoided in Python 2.1 - but that's a new
feature. And besides, the meaning of lambda is still not exactly
obvious.

Now lets go a stage further...


processed_list = [cell_fn (x, y)  for x in range (max_x)
                                  for y in range (max_y)
                                  if cell_valid (x, y)      ]


processed_list = map (lambda j : j [2],
                      filter (lambda i : cell_valid (i[0], i[1]),
                              map (lambda x, y : [x, y, cell_fn (x,
y)],
                                   range (max_x), range (max_y)
                                  )
                             )
                     )


Clearly the list comprehensions remain much clearer as the list
requirements get more complex. It neatly expresses the idea of listing
all the cell_fn results for the valid cells in an x, y grid. Even more
important, it does not need an intermediate data structure to preserve
the x, y, result mapping as the data propogates through various
functions.

Of course no-one would ever write that kind of map/filter/map
expression for real - at least I hope not. I assume you'd switch to
using a couple of nested for loops and an if to build the list. But
IMO that simply doesn't compare to the list comprehension version for
clarity or convenience.

BTW - that last example was actually substantially simplified from a
real example which I don't have the time to translate/debug using map,
filter etc - yet it took no more than 1 minute to code the original as
a list comprehension, it worked first time, and it is as clear as day
to read.

-- 
Steve Horne
Home : steve at lurking.demon.co.uk
Work : sh at ttsoftware.co.uk



More information about the Python-list mailing list