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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4