Main
Main
print(i)
You may not realize it, but you've encountered generators many, many times before.
Take a look at the very simple snippet:
for i in range(5):
print(i)
The range() function is, in fact, a generator, which is (in fact, again) an
iterator.
A function returns one, well-defined value - it may be the result of a more or less
complex evaluation of, e.g., a polynomial, and is invoked once - only once.
In the example, the range() generator is invoked six times, providing five
subsequent values from zero to four, and finally signaling that the series is
complete.
The above process is completely transparent. Let's shed some light on it. Let's
show you the iterator protocol.