]> granicus.if.org Git - python/commitdiff
a much better example
authorBenjamin Peterson <benjamin@python.org>
Wed, 18 Mar 2009 20:58:09 +0000 (20:58 +0000)
committerBenjamin Peterson <benjamin@python.org>
Wed, 18 Mar 2009 20:58:09 +0000 (20:58 +0000)
Doc/library/functions.rst

index 427f864a37a64ea29cf14e4d67479eefeb61d554..0497d456b0431e1e199c92cdc7c95af5f8c8d5ae 100644 (file)
@@ -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