Should a Python generator raise an exception when there are no more elements to yield?
Which one?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With