September 2017
Beginner
244 pages
5h 20m
English
Generators give the functionality of iterators but without having to write an entire class. In many cases, the logic of creating the elements of a sequence are implemented in the next function of an iterator. For example, the code to read a text file line by line and return the occurrence of a specific word in each line needs to be written in the next function of an iterator. Other functions such as __init__ and __iter__ are implemented in the class merely to suffice the requirements of developing an iterator.
Generators are special functions that simplify development. The yield keyword in a function signals the Python interpreter that the function is a generator. Using generator functions, we implement only the logic of creating ...