Skip to end of metadata
Go to start of metadata

Generator methods are defined by the use of the yield keyword:

Given the definition above the following program would print the first five elements of the Fibonacci series:

So although the generator definition itself is unbounded (a while true loop) only the necessary elements will be computed, five in this particular case as the zip builtin will stop asking for more when the range is exausted.

Generator methods are also a great way of encapsulating iteration logic:

  • No labels