8000 bpo-34764: improve docs example of iter() with sentinel value (GH-11222) · python/cpython@d378b1f · GitHub
[go: up one dir, main page]

Skip to content

Commit d378b1f

Browse files
chris-randsrhettinger
authored andcommitted
bpo-34764: improve docs example of iter() with sentinel value (GH-11222)
1 parent 8874f51 commit d378b1f

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
@@ -810,13 +810,14 @@ are always available. They are listed here in alphabetical order.
810810

811811
See also :ref:`typeiter`.
812812

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

821822

822823
.. 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