readlines()是否在Python 3中返回列表或迭代器?
问题内容:
我在“深入Python 3”中已经读到“ readlines()方法现在返回一个迭代器,因此它的效率与Python 2中的xreadlines()一样。”
参见此处:http : //diveintopython3.org/porting-code-to-
python-3-with-2to3.html
。我不确定这是真的,因为他们在这里没有提及:http
:
//docs.python.org/release/3.0.1/whatsnew/3.0.html。我该如何检查?
问题答案:
像这样:
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.
>>>