9.1 Writing generator functions with the yield statement
A generator function is often designed to apply some kind of transformation to each item of a collection. Generators can create data, too. A generator is called lazy because the values it yields must be consumed by a client; values are not computed until a client attempts to consume them. Client operations like the list() function or a for statement are common examples of consumers. Each time a function like list() demands a value, the generator function must yield a value using the yield statement.
In contrast, an ordinary function can be called eager. Without the yield statement, a function will compute the entire result and return it via the return statement.
A lazy approach is very helpful in cases where we can’t fit an entire collection in memory. For example, analyzing gigantic web log files can be done in small doses rather than by creating a vast in-memory collection.
In the language...