Why are "broken iterators" broken?

Lie Lie.1296 at gmail.com
Sun Sep 28 17:19:15 EDT 2008


On Sep 21, 10:13 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> According to the Python docs, once an iterator raises StopIteration, it
> should continue to raise StopIteration forever. Iterators that fail to
> behave in this fashion are deemed to be "broken":
>
> http://docs.python.org/lib/typeiter.html
>
> I don't understand the reasoning behind this. As I understand it, an
> iterator is something like a stream. There's no constraint that once a
> stream is empty it must remain empty forever.

I think empty != StopIteration. StopIteration (IMHO) shouldn't be
raised when the stream is empty, instead a sentinel value specifying
that "there is no data yet, but if you wait there might be" should be
returned (possibly None or empty string). When you raise
StopIteration, it is a signal that I don't have any more data and
there is no use in waiting.

> Can somebody explain why "broken iterators" are broken?
>
> --
> Steven




More information about the Python-list mailing list