mirror of
https://github.com/AdaCore/cpython.git
synced 2026-02-12 12:57:15 -08:00
bpo-34764: improve docs example of iter() with sentinel value (GH-11222) (#11301)
(cherry picked from commit d378b1f8ed)
Co-authored-by: Chris Rands <c_rands100@hotmail.com>
This commit is contained in:
committed by
Raymond Hettinger
parent
bc64123335
commit
00a48d57df
@@ -809,13 +809,14 @@ are always available. They are listed here in alphabetical order.
|
||||
|
||||
See also :ref:`typeiter`.
|
||||
|
||||
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 the :meth:`~io.TextIOBase.readline` method returns an empty string::
|
||||
One useful application of the second form of :func:`iter` is to build a
|
||||
block-reader. For example, reading fixed-width blocks from a binary
|
||||
database file until the end of file is reached::
|
||||
|
||||
with open('mydata.txt') as fp:
|
||||
for line in iter(fp.readline, ''):
|
||||
process_line(line)
|
||||
from functools import partial
|
||||
with open('mydata.db', 'rb') as f:
|
||||
for block in iter(partial(f.read, 64), ''):
|
||||
process_block(block)
|
||||
|
||||
|
||||
.. function:: len(s)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Improve example of iter() with 2nd sentinel argument.
|
||||
Reference in New Issue
Block a user