Search code examples
pythoniteratorpython-3.xreadlines

Does readlines() return a list or an iterator in Python 3?


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: Appendix A: Porting Code to Python 3 with 2to3: A.26 xreadlines() I/O method.

I'm not sure that'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?


Solution

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