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

python

Solution

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.

Problem

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

Original source