Callbacks to generators
Terry Reedy
tjreedy at udel.edu
Tue Jun 8 09:39:45 EDT 2004
"Humpty Dumpty" <oliver.schoenborn at utoronto.ca> wrote in message
news:_Zhxc.27159$sS2.784227 at news20.bellglobal.com...
> Something weird here. I hven't used generators much, but seems to me
that:
>
> 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
the
> 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