line in iter(fp.readline, '') rather than line in fp:

喜欢而已 提交于 2019-12-11 17:18:15

问题


I read builtin function iter 's example in Built-in Functions — Python 3.7.0 documentation

with open('mydata.txt') as fp:
    for line in iter(fp.readline, ''):
        process_line(line)

I could not figure out what's the advantage over the following:

with open('mydata.txt') as fp:
    for line in fp:
        process_line(line)

Could you please provide any hints?


回答1:


Both will iterate over a generator, without loading the whole file into memory, but the iter() version is demonstrating the use of the second argument of iter(), "sentinel".

From the docs:

if the value returned is equal to sentinel, StopIteration will be raised

So this code will read from the file, until a line equals '' and then stop.

This is a strange example, as all lines in the file will have a newline on the end, so this will only trigger at the end of the file anyway (if at all).




回答2:


As wim and I discussed in the comments, there is no advantage for this particular case. For the 2nd code snippet to be equivalent to the first code snippet then it would look something like this:

with open('mydata.txt') as fp:
    for line in fp:
        if line == '':
            break
        process_line(line)

However, the only case an empty string can be returned by readline is at the end of the file (EOF) so it makes now difference here (other lines contain a newline '\n' character at least).

If rather than an empty string another value was used, then the difference would be meaningful though. Personally, I think the docs should use a better example to illustrate this, like as follows:

>>> f = open('test')
>>> f.read()
'a\nb\nc\n\nd\ne\nf\n\n'
>>> f = open('test')
>>> [line for line in iter(f.readline, 'b\n')]
['a\n']
>>> f = open('test')
>>> [line for line in f]
['a\n', 'b\n', 'c\n', '\n', 'd\n', 'e\n', 'f\n', '\n']

(Note I should really be closing the file handles)

EDIT: I raised this as a possible documentation bug in issue34764



来源:https://stackoverflow.com/questions/52446415/line-in-iterfp-readline-rather-than-line-in-fp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!