Here is an example of a generator in action:
- The line 1 simply creates the function as provided previously.
- The line 2 calls the generator like a normal function, showing that generators can operate exactly like a regular function. You could capture the results in a list object if you wanted a permanent copy of the results.
- The line 3 creates an instance of the generator.
- The lines 4-6 show how a generator is typically used. By calling the generator instance as the argument for next(), the generator processing is paused after each evaluation cycle. Rather than receiving all results at once, only one value is provided from ...