[Python-Dev] Termination of two-arg iter()
Brett Cannon
drifty@bigfoot.com
Sun, 14 Jul 2002 12:57:31 -0700 (PDT)
[Oren Tirosh]
> On Sun, Jul 14, 2002 at 01:16:10AM -0700, Brett Cannon wrote:
> > [Oren Tirosh]
>
> This anthropomorphic description has too many irrelevant associations. Let's
> leave the parents out of this.
=) OK.
> The logic is simple: StopIteration is not an error. It's not even a warning,
> it's a normal part of program operation. It uses the exception mechanism
> because it is the most convenient form of out-of-band signalling. The
> hypothetical IteratorExhausted is an error. The fact that both of them
> happen to be exceptions is almost a coincidence.
>
It still doesn't "feel" right. I completely understand this explanation
for what StopIteration is supposed to be viewed as, but I just don't
naturally view it that way.
And it isn't because it is an exception. I am sure all of us have used
exceptions to break out of some deeply nested loops or pull off some other
fancy control flow. I think my view stems from what it is saying "I have
reached what I believe to be the end or what you have requested to be the
end". I don't think that should be some notice.
> > definitely will be. I know I thought that StopIteration was continuously
> > raised until the emails on this subject started.
>
> For most Python iterators it is. This behavior is OK but it could be changed
<snip>
And if most are already like that, then maybe it should be the default
behavior. Unless my understanding is faulty, two-arg iterators are a
convenience to make an iterator out of a callable function by specifying
where it the iterator will raise StopIteration. My view is that if you
doin't want to stop where the iterator says you said to signal, then an
explicit 'if' would be better. I mean you don't see iterators on lists
raising IndexError if you keep calling .next().
I am obviously +1 on forcing StopIteration to be permanently raised. If
you need to go beyond the signalled end, you can do the old-fashioned way
before we had iterators. I say make iterators so that they have the least
chance of causing errors. They are supposed to simplify our lives not
cause us to have a new possible bug to watch out for in code.
Anyway, since I am not about to come up with some clever code chunk that
will show why the current state of affairs is bad beyond my opinion I will
leave it up to Guido to make a choice. At least, based on the tone of
these emails on this topic, this is not going to be a decision that is
going to ruffle some feathers.
-Brett C.