Callbacks to generators

Terry Reedy tjreedy at
Tue Jun 8 15:39:45 CEST 2004

"Humpty Dumpty" <oliver.schoenborn at> wrote in message
news:_Zhxc.27159$sS2.784227 at
> Something weird here. I hven't used generators much, but seems to me
> 1) you maybe don't need them:
>  def process_iter(text):
>      def on_token(token):
>          return token
>      process(text, on_token)

True.  And one cannot use them for 2.1.  I expect OP wants to use iterators
in 2.2+ to avoid repeated function call overhead.  The main problem is
writing code that works both ways.

> 2) you need some sort of while loop so the fnction doesn't return after
> first yield:

True.  however...

>  def process_iter(text):
>      def on_token(token):
>          while 1:
>             yield token

This is garbled and repeats OP's mistake of making token a param of the
generator function.  A generator function is called once and its parameter
is used to set up the generator whose next method is what gets called
repeatedly.  See my previous post for a possibly workable example.

Terry J. Reedy

More information about the Python-list mailing list