3 ms·
In Python, it’s common to use exceptions for control flow. Even exiting a loop is done via an exception: `StopIteration`.
by pansa2 10mo ago
In Python, it’s common to use exceptions for control flow. Even exiting a loop is done via an exception: `StopIteration`.
- arccy 10mo agoisn't break more normal
- pansa2 10mo agoI’d say it’s more common for iterator-based loops to run to completion than to hit a `break` statement. The `StopIteration` exception is how the iterator signals that completion.
- zephen 10mo agoThe point is that the use of exceptions is built into the language, so, for example, if you write "for something in somegeneratorfunction():" then somegeneratorfunction will signal to the for loop that it is finished by raising this exception.
- ivanyu 10mo agoIt's not "common". You have to deal with StopIteration only when you write an iterator with the low-level API, which is maybe once in the career time for most of developers.