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.
Example: Alphabet iterator
In the chapter about iterators, we developed an iterator that returned the letters of the alphabet, one by one, and then stopped. ...
Get hands-on with 1400+ tech skills courses.