[docs] [issue34764] Improve documentation example for using iter() with sentinel value

ChrisRands report at bugs.python.org
Fri Sep 21 11:41:31 EDT 2018


New submission from ChrisRands <chrisrands0 at 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 at python
components: Documentation
messages: 325995
nosy: ChrisRands, docs at python
priority: normal
severity: normal
status: open
title: Improve documentation example for using iter() with sentinel value

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34764>
_______________________________________


More information about the docs mailing list