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

Giampaolo Rodolà g.rodola at gmail.com
Wed Nov 30 16:42:16 CET 2011


Il 30 novembre 2011 14:59, Peter Otten <__peter__ at web.de> ha scritto:
> Giampaolo Rodolà wrote:
>
>> This is problably too late and I'm probably missing something but
>> given amount of generators/iterators introduced in python 3.X
>> (http://docs.python.org/release/3.0.1/whatsnew/3.0.html#views-and-
> iterators-instead-of-lists)
>> file.readlines() seems a good case where an iterator can be more
>> appropriate than a list.
>> I realized it while writing this recipe:
>> http://code.activestate.com/recipes/577968-log-watcher-tail-f-log/
>> In this specific case, having readlines() yield a single line at a
>> time would save a lot of memory.
>> Maybe we can introduce a new parameter to do this?
>
> You already have an iterator -- the file itself. Why would you need another
> one?
>
> My observation on the Tutor mailing list is that there are no valid uses of
> readlines(). It's just easier to discover the readlines() method than to
> find out that you can iterate over the file directly.
>
> 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. Backwards compatibility be damned
> ;)

Yes, you're right.
Now that you pointed this out I realize that my request isn't actually
necessary.
To be fair, readlines() is different than an iterated file object in
that it provides the sizehint parameter, so it does cover a use case.
Maybe the doc should just mention that unless sizehint parameter is
used the recommended way to get file lines is to iterate against the
file object itself.

Regards,

--- Giampaolo
http://code.google.com/p/pyftpdlib/
http://code.google.com/p/psutil/



More information about the Python-ideas mailing list