Generator: function with yield - call next
We can create a function that has multiple yield expressions inside. We call the function and what we get back is a generator. A generator is also an iterator so we can call the next function on it and it will give us the next yield value.
If we call it one too many times we get a StopIteration exception.
def number(): yield 42 yield 19 yield 23 num = number() print(type(num)) print(next(num)) print(next(num)) print(next(num)) print(next(num))
<class 'generator'> 42 19 23 Traceback (most recent call last): File "simple_generator_next.py", line 11, in <module> print(next(num)) StopIteration