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

前端 未结 3 1206
旧巷少年郎
旧巷少年郎 2021-01-07 16:31

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://di

3条回答
  •  青春惊慌失措
    2021-01-07 16:56

    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())
    
    >>> 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.
    
    >>>
    

提交回复
热议问题