[Python-Dev] New PEP 342 suggestion: result() and allow "return with arguments" in generators (was Re: PEP 342 suggestion: start(), __call__() and unwind_call() methods)

Nick Coghlan ncoghlan at gmail.com
Mon Oct 10 11:02:57 CEST 2005


Greg Ewing wrote:
> Nick Coghlan wrote:
> 
> 
>>Sometimes I miss the obvious. There's a *much*, *much* better place to store 
>>the return value of a generator than on the StopIteration exception that it 
>>raises when it finishes. Just save the return value in the *generator*.
> 
> 
> I'm not convinced that this is better, because it would
> make value-returning something specific to generators.
> 
> On the other hand, raising StopIteration(value) is something
> that any iterator can easily do, whether it's implemented
> as a generator, a Python class, a C type, or whatever.
> 
> Besides, it doesn't smell right to me -- sort of like returning
> a value from a function by storing it in a global rather than
> using a return statement.

Yeah, the various responses have persuaded me that having generators resemble 
threads in that they don't have a defined "return value" isn't a bad thing at all.

Although that means I've gone all the way back to preferring the status quo - 
if you want to pass data back from a generator when it terminates, just use 
StopIteration(result).

I'm starting to think we want to let PEP 342 bake for at least one release 
cycle before deciding what (if any) additional behaviour should be added to 
generators.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list