This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients ChrisRands, docs@python, rhettinger
Date 2018-09-21.18:56:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1537556178.97.0.956365154283.issue34764@psf.upfronthosting.co.za>
In-reply-to
Content
I concur that the readline() example is problematic.  While it succeeds in showing how iter() works, the example itself is not the best way to solve that particular problem.

Here are two possible substitute examples.

1) Simple example that focuses primarily on the behavior of iter() and required little extra knowledge:


    >>> from random import randint
    >>> def roll_dice():
            return randint(1, 6) + randint(1, 6)

    >>> # roll until a seven is seen
    >>> list(iter(roll_dice, 7))
    >>> list(iter(roll_dice, 7))
    [10, 6, 5, 6, 8]

2) Practical application reading binary files in fixed-width blocks for processing with the structure module.  This also teaches how to partial() to produce an arity-zero callable suitable for use with iter().

    >>> Read fixed-width blocks from a database binary file
    >>> from functools import partial
    >>> with open('mydata.db', 'rb') as f:
    	    for block in iter(partial(f.read, 64)):
		print(parse_struct(block))
History
Date User Action Args
2018-09-21 18:56:18rhettingersetrecipients: + rhettinger, docs@python, ChrisRands
2018-09-21 18:56:18rhettingersetmessageid: <1537556178.97.0.956365154283.issue34764@psf.upfronthosting.co.za>
2018-09-21 18:56:18rhettingerlinkissue34764 messages
2018-09-21 18:56:18rhettingercreate