diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-03-18 20:58:09 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-03-18 20:58:09 (GMT) |
commit | 06919a177d30f4459323caa1106825a32c68bb2e (patch) | |
tree | bf5c1cfa36c326b0978ab526371ffbe5ae1e9356 /Doc | |
parent | 6ffe852f903490132bf33acbb91ee4d2d2ef06b8 (diff) | |
download | cpython-06919a177d30f4459323caa1106825a32c68bb2e.zip cpython-06919a177d30f4459323caa1106825a32c68bb2e.tar.gz cpython-06919a177d30f4459323caa1106825a32c68bb2e.tar.bz2 |
a much better example
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functions.rst | 25 |
1 files changed, 7 insertions, 18 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 427f864..0497d45 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -596,24 +596,13 @@ available. They are listed here in alphabetical order. its :meth:`next` method; if the value returned is equal to *sentinel*, :exc:`StopIteration` will be raised, otherwise the value will be returned. - Example usage: :: - - >>> iterator = iter(range(10)) - >>> iterator - <listiterator object at 0x86b50> - >>> iterator.next() - 0 - >>> 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] + 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 + until ``"STOP"`` is reached: :: + + with open("mydata.txt") as fp: + for line in iter(fp.readline, "STOP"): + process_line(line) .. versionadded:: 2.2 |