question about generators

greg greg at cosc.canterbury.ac.nz
Fri Aug 16 04:32:27 EDT 2002


Andrew Koenig wrote:
> At first I wondered if perhaps calling a generator from
> another generator, and not doing anything with the results, should
> automatically yield them, but decided that was too clever.  Still, I
> wondered if there was an easier way of doing this program
> transformation.  So far, apparently there isn't.

Actually, there is a way, sort of, although it doesn't 
involve using Python generators. You can write it in 
so-called "continuation passing style" like this:

  def f(produce_result):
    for ...:
      if ...:
        produce_result(x)
      else:
        ...
        f(produce_result)
        ...

and you call it with a function (or some other callable
object) that does what you want done with each value.

This is very similar to the way Icon works under the
covers.

Greg Ewing



More information about the Python-list mailing list