[Python-Dev] PEP 342 suggestion: start(), __call__() and unwind_call() methods

Piet Delport pjd at satori.za.net
Sat Oct 8 08:20:30 CEST 2005


Nick Coghlan wrote:
> Phillip J. Eby wrote:
>> Nick Coghlan wrote:
> 
[...]
> 
>> Last, but far from least, as far as I can tell you can implement all of 
>> these semantics using PEP 342 as it sits.  That is, it's very simple to 
>> make decorators or classes that add those semantics.  I don't see 
>> anything that requires them to be part of Python.
> 
> 
> Yeah, I've now realised that you can do all of this more simply by doing it 
> directly in the scheduler using StopIteration to indicate when the coroutine 
> is done, and using yield to indicate "I'm not done yet".

Earlier this week, i proposed legalizing "return Result" inside a generator,
and making it act like "raise StopIteration( Result )", for exactly this reason.

IMHO, this is an elegant and straightforward extension of the current
semantics of returns inside generators, and is the final step toward making
generator-based concurrent tasks[1] look just like the equivalent synchronous
code (with the only difference, more-or-less, being the need for appropriate
"yield" keywords, and a task runner/scheduler loop).

This change would make a huge difference to the practical usability of these
generator-based tasks.  I think they're much less likely to catch on if you
have to write "raise StopIteration( Result )" (or "_return( Result )") all the
time.

[1] a.k.a. coroutines, which i don't think is an accurate name, anymore.


More information about the Python-Dev mailing list