Defining generator functions with the yield statement

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. ...

Get Python Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.