[Python-Dev] listcomps vs. for loops

Guido van Rossum guido at python.org
Wed Oct 22 14:45:47 EDT 2003


> 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)
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /