Commit 06919a17 authored by Benjamin Peterson's avatar Benjamin Peterson

a much better example

parent 6ffe852f
...@@ -596,24 +596,13 @@ available. They are listed here in alphabetical order. ...@@ -596,24 +596,13 @@ available. They are listed here in alphabetical order.
its :meth:`next` method; if the value returned is equal to *sentinel*, its :meth:`next` method; if the value returned is equal to *sentinel*,
:exc:`StopIteration` will be raised, otherwise the value will be returned. :exc:`StopIteration` will be raised, otherwise the value will be returned.
Example usage: :: One useful application of the second form of :func:`iter` is to read lines of
a file until a certain line is reached. The following example reads a file
>>> iterator = iter(range(10)) until ``"STOP"`` is reached: ::
>>> iterator
<listiterator object at 0x86b50> with open("mydata.txt") as fp:
>>> iterator.next() for line in iter(fp.readline, "STOP"):
0 process_line(line)
>>> iterator.next()
1
>>> def my_generator():
... for i in range(10):
... yield i
...
>>> iterator = iter(my_generator().next, 7)
>>> iterator
<callable-iterator object at 0x86bb0>
>>> list(iterator)
[0, 1, 2, 3, 4, 5, 6]
.. versionadded:: 2.2 .. versionadded:: 2.2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment