How a Generator Works: Alphabet Iterator

Learn how a generator works and how you can use it to implement your own iterator.

Generator

We saw in the chapter on iterators that it is possible to create your own iterator by defining a class with a couple of specific methods. That method works fine, but it involves a fair bit of boilerplate code and requires the logic of the iterator to be written in an inside out style.

Generators provide a simple method of implementing many types of iterators using a simple syntax and often a more intuitive software flow.

Get hands-on with 1200+ tech skills courses.