3 ms·
This is right; it’s the StopIteration exception (which is fundamental to what a for loop does in Python). Not saying is absolves the for/while else syntax, but
by mlthoughts2018 8y ago
This is right; it’s the StopIteration exception (which is fundamental to what a for loop does in Python).
Not saying is absolves the for/while else syntax, but it’s still using the expected iteration constructs.
- thaumasiotes 8y agoWouldn't that imply that the equivalent of a while/else loop would be a for/except loop, not a for/else loop?
- mlthoughts2018 8y agoAs 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.