[Python-ideas] Is there a reason why file.readlines() doesn't/can't return an iterator?

Masklinn masklinn at masklinn.net
Wed Nov 30 17:18:30 CET 2011


On 2011-11-30, at 16:51 , Éric Araujo wrote:
> Le 30/11/2011 14:59, Peter Otten a écrit :
>> My observation on the Tutor mailing list is that there are no valid
>> uses of readlines().
> I disagree :)  You don’t always read big files, or you may prefer to
> close the file handle early at the cost of memory, or you need a list to
> do some processing that works with lists but not iterators.  FWIW, I use
> readlines a lot to get list of lines for unit tests, for example (in
> case of failure, lists make better diffs than strings).
Then again, the same process should be used here than the one recommended
for map, filter, dict.(keys|values|items) in Python 3: `list(file)` will
yield a list of lines.


More information about the Python-ideas mailing list