On Mon, Nov 17, 2014 at 9:53 AM, Devin Jeanpierre <jeanpierreda@gmail.com> wrote:
[Guido]
> The problem we're trying to fix is unique to generators (thereby also
> implicating generator expressions).

I suppose since you're only fixing generators, then that is literally
the only problem you are trying to fix, but it is more general than
that. I have encountered this sort of problem writing __next__ by hand
in Python -- that is, that bugs inside code I call result in silent
control flow changes rather than a visible exception.

I assume this is something where the __next__() method on your iterator class calls next() on some other iterator and accidentally doesn't catch the StopIteration coming out of it (or, more likely, this happens several calls deep, making it more interesting to debug).

That particular problem is not unique to __next__ and StopIteration -- the same thing can (and does!) happen with __getitem__ and KeyError or IndexError, and with __getattr[ibute]__ and AttributeError.

In all these cases I think there isn't much we can do apart from adding lint rules. If you are writing __next__ as a method on an iterator class, one way or another you are going to have to raise StopIteration when there isn't another element, and similarly __getitem__ has to raise KeyError or IndexError, etc.

In the generator case, we have a better way to signal the end -- a return statement (or falling off the end). And that's why we can even contemplate doing something different when StopIteration is raised in the generator.

--
--Guido van Rossum (python.org/~guido)