[Python-Dev] yield back-and-forth?

Christian Tanzer tanzer at swing.co.at
Fri Jan 20 19:25:10 CET 2006


Guido van Rossum <guido at python.org> 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 main_generator():
>       ...
>       for value in sub_generator():
>           yield value

For small values of `well enough`. I sometimes override a generator in
a subclass and it sucks to add the loop just because the derived
generator wants to add a value at the beginning or end.

> but now that yield can return a value, that value might have to be
> passed into sub_generator(), 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 main_generator():
>       ...
>       yieldthrough sub_generator()
>
> 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.

How about:

  def main_generator():
      ...
      yield * sub_generator()

Ducking-ly yrs,

-- 
Christian Tanzer                                    http://www.c-tanzer.at/



More information about the Python-Dev mailing list