[Python-Dev] Termination of two-arg iter()
Oren Tirosh
oren-py-d@hishome.net
Sun, 14 Jul 2002 00:31:13 -0400
On Sat, Jul 13, 2002 at 07:07:36PM -0400, Guido van Rossum wrote:
> Actually, not. Under "Resolved Issues" the PEP has this:
>
> - Once a particular iterator object has raised StopIteration, will
> it also raise StopIteration on all subsequent next() calls?
> Some say that it would be useful to require this, others say
> that it is useful to leave this open to individual iterators.
At the time this was discussed on the list has anyone considered the
possibility of raising an exception? Something like 'IteratorExhausted'?
If the current definition is ruled to be 'undefined' then an iterator MAY
raise an exception in this case.
Iterable objects can often serve as a replacement for lists in many places
and even passed successfully to a lot of old code that was written before
the iteration protocol. But an iterable object is not always a suitable
replacement for a sequence when the code needs to iterate multiple times and
the object is not re-iterable. This will fail in a very nonobvious way
without raising an exception because an exhausted iterator looks just like
an empty sequence to a for loop.
I think this kind of errors should not pass silently.
Yes, I have been bitten by this. Perhaps this was a result of overzealous
use of iterators because I was so excited with them, but it's a real
problem, not some contrived example.
Oren