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

Eli Bendersky eliben at gmail.com
Wed Nov 30 14:52:52 CET 2011

On Wed, Nov 30, 2011 at 15:32, Giampaolo Rodolà <g.rodola at gmail.com> 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?

Giampaolo, perhaps I'm missing something, but what's bad about having a
generator instead:

linesgen = (line for line in file)

You'd basically now do with linesgen whatever you wanted to do with an
iterator-returning file.readlines()

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20111130/9d8a4f2c/attachment.html>

More information about the Python-ideas mailing list