[Python-Dev] listcomps vs. for loops

Alex Martelli aleaxit at yahoo.com
Sat Oct 25 09:18:22 EDT 2003


On Wednesday 22 October 2003 08:45 pm, Guido van Rossum wrote:
> > sum(len(line) for line in file if not line.startswith("#") while
> > line.strip())
> >
> > looks simple than
> >
> > sum(itertools.takewhile(lambda l: l.strip(), len(line) for line in file
> > if not line.startswith("#"))
>
> I think both are much harder to read and understand than
>
>   n = 0
>   for line in file:
>       if not line.strip():
>           break
>       if not line.startwith("#"):
>           n += len(line)

Yes, but personally I would prefer another refactoring yet, something
like:

def noncomment_lines_until_white(file):
    for line in file:
        if not line.strip(): break
        if not line.startswith('#'): yield line
n = sum(len(line) for line in noncomment_lines_until_white(file))

To me, the concept "get all non-comment lines until the first
all-whitespace one" gets nicely "factored out", this way, from the
other concept of "sum the lengths of all of these lines".  In Guido's
version I have to reconstruct these two concepts "bottom-up" from
their entwined expression in lower-level terms; in Walter's, I have to
reconstruct them by decomposing a very dense equivalent, still
full of lower-level constructs.  It seems to me that, by naming the
"noncomment_lines_until_white" generator, I make the separation
of (and cooperation between) the two concepts, most clear.

Clearly, people's tastes in named vs unnamed, and lower-level
vs higher-level expression of concepts, differ widely!-)


Alex




More information about the Python-Dev mailing list