3 ms·
As a matter of trivia, probably not. The iteration mechanism just happens to check for StopIteration, and does not automatically handle any other type of Except
by mlthoughts2018 8y ago
As a matter of trivia, probably not. The iteration mechanism just happens to check for StopIteration, and does not automatically handle any other type of Exception, which would require a usual try block.
So StopIteration can be thought of as an equivalent idea to “emptiness” of an iterator, either an empty container, an exhausted generator, etc.
And since emptiness is most often handled as falsiness in a bool context, it’s in some sense the natural notion of a “false” iteration condition.
This is all incredible mental gymnastics though. But (speaking as someone who loves Python) there’s unfortunately a lot of that in Python.