0

The iter() method creates an object which can be iterated one element at a time.

These objects are useful when coupled with loops like for loop, while loop.

The syntax of iter() method is:

iter(object[, sentinel])

As an example using the sentinel bit of this code

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

and so the stop value is a blank line. But the , inside the [ ] array notation is confusion. Clearly the , comes between the object in the sentinel in my example and what would be more a complex example of code that would clarify this kind of use of a sentinel?

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
RhythmInk
  • 513
  • 1
  • 4
  • 18
  • 1
    Read the `[]` as meaning that it is an optional parameter, not an array. So the `iter` takes either just an object, or an object followed by a comma and a sentinel. – jpw Nov 02 '18 at 02:30
  • 1
    [ ] in `iter(object[, sentinel])` is not array notation. It just says you may omit sentinel (plus the comma before it). – Hoblovski Nov 02 '18 at 02:30

0 Answers0