[Python-3000] Optimising nested generators

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Mar 16 07:26:13 CET 2008


Talin wrote:
> 
> if a generator is yielding the 
> entire output of another generator, it might be possible to 'cut out the 
> middleman' and have the ultimate consumer of the values read the 
> innermost generator directly.

On the face of things, it certainly seems as though such a thing
*ought* to be possible. It would require keeping a stack of
generators somewhere, although I'm not sure exactly where.

Perhaps each generator frame could have a "referral" slot
which can contain a reference to another iterator. When a
generator hits a "yield *" statement, it puts the argument
into its referral slot.

Then, the next() method of a generator would first run
down the chain of referral slots until it gets to the
end (either a generator with nothing in its referral slot,
or a non-generator iterator), and resume the iterator found
there. If it's found to be exhausted, you would back up to
the previous one and resume that, etc., until you either
get a value or the outermost generator is exhausted.

Does that sound like it would be workable?

-- 
Greg


More information about the Python-3000 mailing list