New submission from ChrisRands <chrisran...@gmail.com>:

This arose from this SO question: 
https://stackoverflow.com/questions/52446415/line-in-iterfp-readline-rather-than-line-in-fp
 

The example given in the docs:

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

Is exactly equivalent to the following because an empty string is only returned 
by readline at the EOF:

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

The empty string '' sentinel value could be changed to another value to provide 
a possibly more meaningful example where the 2nd code snippet would not always 
produce the same result?

----------
assignee: docs@python
components: Documentation
messages: 325995
nosy: ChrisRands, docs@python
priority: normal
severity: normal
status: open
title: Improve documentation example for using iter() with sentinel value

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue34764>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to