Converting "normal" functions into generators

Alex Martelli aleax at aleax.it
Mon Jan 14 08:20:31 EST 2002


"Chris Wright" <caw at cs.mu.oz.au> wrote in message
news:3C42C150.CCF66201 at cs.mu.oz.au...
> I'd like to be able to convert any arbitary function into a generator,
> "yield"-ing some constant after each statement.
    ...
> I want to do this so that I can have a collection of functions and call
> them one at a time, and have them execute one statement at a time.
> Threads don't give me the control I need.

No, but sys.settrace might.  However, your approach also makes sense.


> Is there a sensible way to do this? (I've read the library references
> regarding the modules that let me play with the AST of a piece of python
> code...and I was hoping for a more simple solution!)

Module tokenize lets you easily distinguish logical line continuations
(then, tokenize generates token NL for the "nonterminating" newlines)
from ends of logical lines (then, tokenize generates token NEWLINE for
the "terminating" newlines).  That may be all you need, perhaps?


Alex






More information about the Python-list mailing list