stopiteration python | python iterators

Опубликовано: 16 Ноябрь 2022
на канале: Coding Study Point
301
like

StopIteration
The example above would continue forever if you had enough next() statements, or if it was used in a for loop.
To prevent the iteration to go on forever, we can use the StopIteration statement
In the __next__() method, we can add a terminating condition to raise an error if the iteration is done a specified number of times:
Stop after 15 iterations: