Callbacks to generators
Terry Reedy
tjreedy at udel.edu
Tue Jun 8 09:52:23 EDT 2004
"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:ca49lu$r31$1 at sea.gmane.org...
> #select one on following depending on Python version
>
> def on_token(): # modified traditional call_back
> token = <get token from token_stash>
> return <some func of token>
>
> def on_token_gen(): # generator version for 2.2+
> while 1:
> token = <get token from token_stash>
> yield <some func of token>
> on_token = on_token_gen().next
>
> process(text, on_token)
>
> where process puts token in token_stash before calling on_token()
If doing lots of callbacks, repeating anything like the above for each
would get pretty tedious. So after getting the pattern correct, if the
above does indeed work, I might try to factor out the common portion and
write a callback factory function that inserts the variable code into the
version-appropriate template, compiles it, and returns the
version-apprieate callback.
Terry J. Reedy
More information about the Python-list
mailing list