The presence of the
statement in a
function means that the function is actually a generator object, and
will have the an iterator-like interface built automatically. In effect
it becomes a stateful object with a
defined — so it will work with the
and it will raise the
exception when it returns.
The syntax for a function definition is in the section called “Function Definition: The
Statements”; a generator is similar.
The suite of statements must include at least one
statement specifies the values
emitted by the generator. Note that the expression is required.
statement is used, it ends the
generator, and raises the
exception to alert the
statement. For obvious
statement cannot return a
Here's a complete, but silly example of a generator.
In this case, we simply yield a fixed sequence of values. After
yielding five values, it exits. Here's how this generator is used by a
for p in primes():
... print p