An iteration idiom (Was: Re: [Guppy-pe-list] loading files containing multiple dumps)

Raymond Hettinger python at rcn.com
Wed Sep 2 15:34:12 EDT 2009


> I dont know guppy,
> but if h.load(f) raises StopIteration upon eof, as seems implied by your
> proposal, then something like the following would work.
>
> sets.extend(h.load(f) for _ in xrange(1e9))

Sounds like hpy has a weird API.  Either it should be an
iterator supporting __iter__() and next() and raising
StopIteration when it's done, or it should simply return
None to indicate an empty load.

In the first case, you would write:
   sets.extend(h.load(f))

And in the second case:
   sets.extend(iter(partial(h.load, f), None))

The first way just uses the iterator protocol in a way that
is consistent with the rest of the language.

The second way, using the two argument form of iter(),
is the standard way of creating an iterator from a
function that has a sentinel return value.

IOW, it is not normal to use StopIteration in a function
that isn't an iterator.


Raymond



More information about the Python-list mailing list