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

Peter Otten __peter__ at web.de
Wed Nov 30 17:38:29 CET 2011


É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.

> 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)?

>> So my counter-proposal is to remove the readlines method but to leave the
>> corresponding entry in the documentation with a a description of the
>> "right way" to iterate over the lines of a file.
> A doc entry for a non-existing function would be very confusing.
> 
>> 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.




More information about the Python-ideas mailing list