Converting "normal" functions into generators
Chris Wright
caw at cs.mu.oz.au
Mon Jan 14 06:30:24 EST 2002
I'd like to be able to convert any arbitary function into a generator,
"yield"-ing some constant after each statement.
for the simple case:
def easy(x):
y = x / 2
z = y / 3 + 5
if y > 10:
return y
else:
return x
would turn into:
def easy(x):
y = x / 2
yield AConstantValue
z = y / 3 + 5
yield AConstantValue
if y > 10:
return y
else:
return x
A simple regexp substitution might be difficult:
eg:
def plain(x):
d = { 1 : "one",
2 : "two"
}
y = x + \
1
could be tricky
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.
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!)
cheers and thanks
Chris Wright
More information about the Python-list
mailing list