[Python-Dev] Proposal for 2.5: Returning values from PEP 342 enhanced generators

Christopher Armstrong radeex at gmail.com
Tue Oct 4 04:03:41 CEST 2005


On 10/4/05, Piet Delport <pjd at satori.za.net> wrote:
> One system that could benefit from this change is Christopher Armstrong's
> defgen.py[1] for Twisted, which he recently reincarnated (as newdefgen.py) to
> use enhanced generators. The resulting code is much cleaner than before, and
> closer to the conventional synchronous style of writing.
>
> [1] the saga of which is summarized here:
>     http://radix.twistedmatrix.com/archives/000114.html
>
> However, because enhanced generators have no way to differentiate their
> intermediate results from their "real" result, the current solution is a
> somewhat confusing compromise: the last value yielded by the generator
> implicitly becomes the result returned by the call. Thus, to return
> something, in general, requires the idiom "yield Foo; return". If valued
> returns are allowed, this would become "return Foo" (and the code implementing
> defgen itself would probably end up simpler, as well).

Hey, that would be nice. I've found people confused by the way defgen
handles return values before, getting seemingly meaningless values out
of their defgens (if the defgen didn't specifically yield some
meaningful value at the end).

At first I thought "return foo" in a generator ought to be equivalent
to "yield foo; return", but at least for defgen, it turns out raising
StopIteration(foo) would be better, as I would have a very explicit
way to specify and find the return value of the generator.


--
  Twisted   |  Christopher Armstrong: International Man of Twistery
   Radix    |    -- http://radix.twistedmatrix.com
            |  Release Manager, Twisted Project
  \\\V///   |    -- http://twistedmatrix.com
   |o O|    |
w----v----w-+


More information about the Python-Dev mailing list