views:

207

answers:

2

I've read in "Dive into Python 3" that "The readlines() method now returns an iterator, so it is just as efficient as xreadlines() was in Python 2". See here: http://diveintopython3.org/porting-code-to-python-3-with-2to3.html . I'm not sure that it's true because they don't mention it here: http://docs.python.org/release/3.0.1/whatsnew/3.0.html . How can I check that?

+3  A: 

The readlines method doesn't return an iterator in Python 3, it returns a list

Help on built-in function readlines:

readlines(...)
    Return a list of lines from the stream.

To check, just call it from an interactive session - it will return a list, rather than an iterator:

>>> type(f.readlines())
<class 'list'>

Dive into Python appears to be wrong in this case.


xreadlines has been deprecated since Python 2.3 when file objects became their own iterators. The way to get the same efficiency as xreadlines is instead of using

 for line in f.xreadlines():

you should use simply

 for line in f:

This gets you the iterator that you want, and helps to explain why readlines didn't need to change its behaviour in Python 3 - it can still return a full list, with the line in f idiom giving the iterative approach, and the long-deprecated xreadlines has been removed completely.

Scott Griffiths
@Scott Griffiths: I would expect that anybody who was using `for line in f.xreadlines():` will have converted it to `for line in f:` years ago.
John Machin
@John Machin: I'd hope so too (though perhaps not expect it!) I didn't mean to imply that `xreadlines` was the preferred way to do it in Python 2 - it was deprecated way back in Python 2.3 when the file-as-iterator method was introduced. I just meant to give the reason it could be removed in Python 3.
Scott Griffiths
+7  A: 

Like this:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('/junk/so/foo.txt')
>>> type(f.readlines())
<class 'list'>
>>> help(f.readlines)
Help on built-in function readlines:

readlines(...)
    Return a list of lines from the stream.

    hint can be specified to control the number of lines read: no more
    lines will be read if the total size (in bytes/characters) of all
    lines so far exceeds hint.

>>>
John Machin