Question on style.

David Bolen db3l at fitlinxx.com
Wed Sep 13 00:52:36 CEST 2000


Paul Duffin <pduffin at hursley.ibm.com> writes:

> IMO the long version is easier to read than the short version because 
> there is less magic going on. The more you use something such as list 
> comprehensions the easier it will become to understand them.

There is also a tradeoff between complexity of a single expression and
our (as in humans) difficulty processing a large amount of information
at one time.  Independent of the functionality of list comprehensions
(in terms of acting as expressions, etc...) this is also important to
their readability.

So, while a list comprehension may require more effort to visually and
intellectually parse than any of the equivalent individual steps, at
some point the number of individual steps is large enough that the
extra overhead to mentally track the larger amount of information is
larger than that to grasp the list comprehension.

Whether or not this tradeoff is for a single comprehension or
something more complicated may in fact be different for different
individuals, but I wouldn't generally say that having less magic per
statement makes it easier to read.  Of course, we don't want to end up
at the other end of the P-language-pond in this respect, but as with
other Python features, I think there's a line to tread.

> Any one like to comment on why this particular order was chosen over
> the more 'readable' one of for first then expression.

I see it as emphasizing the nature of a list comprehension.  The
construct is a list, so the first element tells you what comprises the
list.  In a practical sense, it's closer to other functional languages
(from whence it borrows), as well as mathematical notation.

In the example, it's pretty easy to see from the initial "[[" that
you're dealing with a list of lists as your final expression value,
for example, which I don't think is as easy to see at a glance from
the longer expression.

> How would you 'say' the above list comprehension ?

Since I'm not sure how to "say" the longer form either in any concise
way, I don't know if this is relevant to readability, but given the
original comprehension of:

    [[score[frame] for score in scores] for frame in range(3)]

how about something like "The list of lists of score[frame] where
score in scores and frame in range(3)".

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list