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.

Author terry.reedy
Recipients docs@python, louiscipher, mgrazebrook, rhettinger, terry.reedy
Date 2011-05-05.20:37:11
SpamBayes Score 3.14738e-11
Marked as misclassified No
Message-id <1304627831.93.0.475762062243.issue11163@psf.upfronthosting.co.za>
In-reply-to
Content
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
History
Date User Action Args
2011-05-05 20:37:11terry.reedysetrecipients: + terry.reedy, rhettinger, docs@python, mgrazebrook, louiscipher
2011-05-05 20:37:11terry.reedysetmessageid: <1304627831.93.0.475762062243.issue11163@psf.upfronthosting.co.za>
2011-05-05 20:37:11terry.reedylinkissue11163 messages
2011-05-05 20:37:11terry.reedycreate