Use-cases for alternative iterator

Hrvoje Niksic hniksic at xemacs.org
Fri Mar 11 02:19:42 EST 2011


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> I've never seen this second form in actual code. Does anyone use it,
> and if so, what use-cases do you have?

Since APIs that signal end-of-iteration by returning a sentinel have
fallen out of favor in Python (with good reason), this form is rare, but
still it's sometimes useful.  I've used it in actual code for reading a
file in fixed-size chunks, like this:

for chunk in iter(lambda: f.read(CHUNK_SIZE), ''):
    ...



More information about the Python-list mailing list