[Python-Dev] return from a generator [was:PEP 380 (yield from a subgenerator) comments]

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Mar 27 07:45:05 CET 2009

Jim Jewett wrote:

> I still don't see why it needs to be a return statement.  Why not make
> the intent of g explicit
>     def g():
>         yield 42
>         raise StopIteration(43)

Because it would be tedious and ugly, and would actually make
the intent *less* clear in the intended use cases. When
writing a suspendable function, you're not thinking about
iteration. You're thinking about writing a function that
performs some computation and possibly returns a value
when finished.

Have you seen my multithreaded server example? Do you
really think the intent of the code would be any clearer
if all the returns were replaced by raising StopIteration?


More information about the Python-Dev mailing list