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

James Y Knight foom at fuhm.net
Sat Oct 8 04:50:11 CEST 2005


On Oct 3, 2005, at 1:53 AM, Piet Delport wrote:
> For generators written in this style, "yield" means "suspend  
> execution of the
> current call until the requested result/resource can be provided", and
> "return" regains its full conventional meaning of "terminate the  
> current call
> with a given result".
>
> The simplest / most straightforward implementation would be for  
> "return Foo"
> to translate to "raise StopIteration, Foo". This is consistent with  
> "return"
> translating to "raise StopIteration", and does not break any existing
> generator code.
>
> (Another way to think about this change is that if a plain  
> StopIteration means
> "the iterator terminated", then a valued StopIteration, by  
> extension, means
> "the iterator terminated with the given value".)
>

It sounds like a nice idea to me. Of course, it is only useful to  
functions calling ".next()" explicitly; in something like a for loop,  
the return value would just be ignored.

James


More information about the Python-Dev mailing list