June 2015
Intermediate to advanced
298 pages
7h 15m
English
A generator function has properties similar to a generator expression. Rather than a single expression, a generator function is a full Python function. It has all of the features of the functions described in Chapter 7, Basic Function Definitions. It has the additional characteristic of being an iterator, capable of generating a sequence of items.
When we use a yield statement, the semantics of the function are changed. Without a yield, a function will return a single value. With a yield statement, a function will behave like an iterator, providing multiple values to a consumer.
Here's an example of a generator function that applies a range of values to a model to compute a domain of results. ...
Read now
Unlock full access