[Python-Dev] yield back-and-forth? (original) (raw)

Phillip J. Eby pje at telecommunity.com
Fri Jan 20 20:05:30 CET 2006


At 10:17 AM 01/20/2006 -0800, Guido van Rossum wrote:

The discussion about PEP 343 reminds me of the following. Bram Cohen pointed out in private email that, before PEP 342, there wasn't a big need for a shortcut to pass control to a "sub-generator" because the following for-loop works well enough:

def maingenerator(): ... for value in subgenerator(): yield value but now that yield can return a value, that value might have to be passed into subgenerator(), not to mention of exceptions. I'm sure there's a way to write that (although I haven't found the time to figure it out) but I expect it to be cumbersome and subtle. I don't think a helper function can be created to solve this problem, because the yield syntax is essential. Bram suggested the following syntax: def maingenerator(): ... yieldthrough subgenerator() I'm not keen on that particular keyword, but I do believe a syntactic solution is needed, if the problem is important enough to be solved.

What's the use case for this? In the coroutine use case, the PEP 342 sample trampoline takes care of this. If you yield a sub-generator (i.e. 'yield sub_generator()'), the trampoline effectively replaces the parent with the child until the child is complete.

So, that leaves only non-coroutine use cases, and I'm having a hard time thinking of any where there would be bidirectional communication.

Thoughts?

If we have to have a syntax, "yield from sub_generator()" seems clearer than "yieldthrough", and doesn't require a new keyword.



More information about the Python-Dev mailing list