comprehensions was Re: Switch statements again

Dave Brueck dave at pythonapocrypha.com
Thu Jan 16 18:39:02 CET 2003


On Thu, 16 Jan 2003, Sean 'Shaleh' Perry wrote:

> On Thursday 16 January 2003 07:24, A. Lloyd Flanagan wrote:
> > Jack Diederich <jack at performancedrivers.com> wrote in message
> > news:<mailman.1042672181.22990.python-list at python.org>...
> >
> > > using a list comprehension or zip().  Comprehensions always
> > > hurt readability (ALWAYS), and we don't need the speedup
> > > here so we do it explicitly.
> >
> > Personally, I find list comprehensions to be both very readable and
> > compact.  Maybe it's a matter of how familiar you are with them.
[snip example]

> > after all, a list comprehension is less general than a for-loop, so
> > you know more about what it does before you read it.
>
> comprehensions are hard on the reader until they start using them.  I am in
> the archives of this and -tutor stating my disklike for them for this reason.
> Now that I routinely use them in my own code I do like them but there is a
> significant penalty to the new Pythoner.

But why single out comprehensions? Isn't this true for _any_ feature/idiom
you're not used to? I find comprehensions to be very readable, so I use
them a lot, so I find them more readable, but IMO they were also pretty
readable from the get-go.

In the specific case of comprehensions, I always have to "slow down" when
reading code that instead one of the builtin functions (e.g. map, filter)
because to me they are far less clear. I know I'm comparing syntax to a
builtin, but given a sequence, S, the code:

[x for x in S if x > 5]

always makes far more sense to me than:

filter(lambda x: x>5, S)

for various reasons, including the fact "for x in S", "if x > 5", and
"[...stuff...]" all mean something to me already whereas filter(someArg1,
someArg2) does not, and lambda requires me to think more. :)

FWIW, I found simple list comprehensions to be one of the least taxing
Pythonisms to get used to - IMO the syntax maps very obviously to the
behavior.

Just my two cents,
-Dave





More information about the Python-list mailing list