10000 bpo-34764: improve docs example of iter() with sentinel value (GH-112… · python/cpython@00a48d5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00a48d5

Browse files
miss-islingtonchris-rands
authored andcommitted
bpo-34764: improve docs example of iter() with sentinel value (GH-11222) (#11301)
(cherry picked from commit d378b1f) Co-authored-by: Chris Rands <c_rands100@hotmail.com>
1 parent bc64123 commit 00a48d5

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

Doc/library/functions.rst

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,14 @@ are always available. They are listed here in alphabetical order.
809809

810810
See also :ref:`typeiter`.
811811

812-
One useful application of the second form of :func:`iter` is to read lines of
813-
a file until a certain line is reached. The following example reads a file
814-
until the :meth:`~io.TextIOBase.readline` method returns an empty string::
815-
816-
with open('mydata.txt') as fp:
817-
for line in iter(fp.readline, ''):
818-
process_line(line)
812+
One useful application of the second form of :func:`iter` is to build a
813+
block-reader. For example, reading fixed-width blocks from a binary
814+
database file until the end of file is reached::
815+
816+
from functools import partial
817+
with open('mydata.db', 'rb') as f:
818+
for block in iter(partial(f.read, 64), ''):
819+
process_block(block)
819820

820821

821822
.. function:: len(s)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve example of iter() with 2nd sentinel argument.

0 commit comments

Comments
 (0)
0