Why are "broken iterators" broken?

Terry Reedy tjreedy at udel.edu
Sun Sep 21 15:22:42 EDT 2008


Steven D'Aprano 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.

It is quite possible that a stream reader will return '' on one call and 
  then something non-empty the next.  An iterator that reads a stream 
and yields chunks of whatever size should either block until it gets 
sufficient data or yield nulls as long as the stream is open and not 
raise StopIteration until the steam is closed and it has yielded the 
last chunk of data.

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

There is an important different between a store that is closed until the 
next day and one that closed - out of business.  Similarly, there is a 
difference between an item being out-of-stock until the next delivery 
and out-of-stock and discontinued permanently, or between a road closed 
for repairs versus removal for something else.  Using the same sign or 
signal for temporary and permanent conditions is confusing and therefore 
  'broken'.

Terry Jan Reedy




More information about the Python-list mailing list