homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: iter() documentation code doesn't work
Type: behavior Stage: needs patch
Components: Documentation Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, louiscipher, mgrazebrook, purplezephyr, rhettinger, terry.reedy
Priority: low Keywords: patch

Created on 2011年02月09日 20:40 by mgrazebrook, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue11163.py33.patch louiscipher, 2011年05月05日 20:00 review
Messages (7)
msg128238 - (view) Author: Michael Grazebrook (mgrazebrook) Date: 2011年02月09日 20:40
This code fragment from the documentation of iter() doesn't work as intended. Change "STOP" to "STOP\n". Maybe also check for EOF as it hangs.
with open("mydata.txt") as fp:
 for line in iter(fp.readline, "STOP"):
 process_line(line)
Or maybe this makes a better example because it's clearer wha'ts going on:
square_generator = (i * i for i in range(100000))
for n in iter( square_generator.next, 144):
 print n
msg135238 - (view) Author: Bryce Verdier (louiscipher) Date: 2011年05月05日 20:00
Here is the patch to fix the documentation. Asked some core developers off the bug tracker how to handle this bug in relation to the bigger issue regarding "STOP" leading to an infinite loop.
msg135241 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011年05月05日 20:37
I think this is the wrong patch for reasons given below.
The example should be replaced instead.
Readline is documented as returning '' at EOF for text files.
Iter(func,sentinel) is documented as calling func until sentinel is returned. If that never happens, it never stops. That makes it dangerous unless one KNOWS for sure that the sentinel WILL be returned or is willing to continue indefinitely. I think a sentence should be added to the doc to warn about this.
So I consider the premise of the example, "One useful application of the second form of iter() is to read lines of a file until a certain line is reached.", to be somewhat dubious. The example could be considered instead to be an example of when NOT to use the second form. This application should better be written to avoid a possible infinite loop as
with open(name) as fp:
 for line in fp:
 if line == sentinel:
 break
 process(line)
I think a better example would be (3.2 version, use raw_input for 2.7):
def my_input():
 return input("Enter data or return to stop: ")
for data in iter(my_input, ''):
 process(data)
Your alternative also works, but the above is something one might actually do. Even that is hardly much better than:
while True:
 data = input("Enter data or return to stop}
 if not data: break
 process data
msg135246 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011年05月05日 21:00
I'll replace this with a better example using binary chunked reads that terminate with an empty string.
msg139124 - (view) Author: Michael Grazebrook (mgrazebrook) Date: 2011年06月25日 22:19
Thank you.
On 25/06/2011 13:38, Raymond Hettinger wrote:
> Changes by Raymond Hettinger<raymond.hettinger@gmail.com>:
>
>
> ----------
> resolution: -> fixed
> status: open -> closed
>
> _______________________________________
> Python tracker<report@bugs.python.org>
> <http://bugs.python.org/issue11163>
> _______________________________________
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1388 / Virus Database: 1513/3725 - Release Date: 06/25/11
>
>
>
msg197500 - (view) Author: (purplezephyr) Date: 2013年09月11日 18:03
This does not seem to have been changed in any version, per msg135246. If it's not going to be replaced, there's another issue, which is that the link to readline() in the text is incorrect - it goes to the readline module, not file.readline().
msg197505 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2013年09月11日 19:47
In the current 3.3.2 docs, 'STOP' has been replaced by '', so there is a change, and no infinite loop. However, this is still does not strike me as an example of 'useful' as 
 for line in iter(fp.readline, "STOP"): # is a bad version of
 for line in fp:
It does illustrate the behavior though.
History
Date User Action Args
2022年04月11日 14:57:12adminsetgithub: 55372
2013年09月11日 19:47:47terry.reedysetstage: needs patch
messages: + msg197505
versions: + Python 3.4, - Python 3.1, Python 3.2
2013年09月11日 18:03:41purplezephyrsetnosy: + purplezephyr
messages: + msg197500
2011年06月25日 22:19:25mgrazebrooksetmessages: + msg139124
2011年06月25日 12:38:57rhettingersetstatus: open -> closed
resolution: fixed
2011年05月05日 21:00:40rhettingersetpriority: normal -> low

messages: + msg135246
2011年05月05日 20:58:58rhettingersetassignee: docs@python -> rhettinger
2011年05月05日 20:37:24terry.reedysetversions: + Python 3.1, Python 2.7, Python 3.2, Python 3.3, - Python 2.6
2011年05月05日 20:37:11terry.reedysetnosy: + terry.reedy
messages: + msg135241
2011年05月05日 20:00:11louisciphersetfiles: + issue11163.py33.patch

nosy: + louiscipher
messages: + msg135238

keywords: + patch
2011年02月09日 23:06:09pitrousetnosy: + rhettinger
2011年02月09日 20:40:44mgrazebrookcreate

AltStyle によって変換されたページ (->オリジナル) /