Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a Python generator raise an exception when there are no more elements to yield?

Tags:

python

Should a Python generator raise an exception when there are no more elements to yield?

Which one?

like image 469
Juanjo Conti Avatar asked Feb 23 '10 22:02

Juanjo Conti


1 Answers

The only time I know that you have to manually raise StopIteration is when you are implementing a next() method on a class to signal that the iterator is terminated. For generators (functions with yield statements in them), the end of the function or a return statement will properly trigger the StopIteration for you.

like image 164
Shane Holloway Avatar answered Oct 15 '22 18:10

Shane Holloway