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

Éric Araujo merwok at netwok.org
Wed Nov 30 17:46:46 CET 2011


Hi Peter,

Le 30/11/2011 17:38, Peter Otten a écrit :
> É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 :)
> Not really, I'm relaying observations made on a list mostly frequented by 
> newbies rather than giving my opinion.
Ah, I misunderstood.

>> 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).
> If there were no readlines() method, would you lobby its introduction or go 
> with list(file)?
I’d use list, if only because I need to support old Python versions <wink>.

>>> Backwards compatibility be damned ;)
>> Not sure what the smiley means; compatibility is taken seriously, and
>> removing things has a very high bar.
> I wanted to express that the change would be good for newbies, but that I 
> don't expect it to happen.
Okay.  You can open a report on bugs.python.org to ask that the doc for
readlines mention list(fp) as an alternative.

Regards



More information about the Python-ideas mailing list