Changing an AST

beza1e1 andreas.zwinkau at
Mon Oct 10 12:54:50 CEST 2005

Is it possible compiler.parse a statement, then change and then
execute/resolve it?

I'm probably to lazy to write my own parser.

I have such a statement as string: "distance = x**2 + y**2"
x and y are undefined, so it is no executable Python code, but it is
parseable. Now i'd like traverse through the AST and change Name('x')
for the value i have elsewhere. And finally let Python resolve the

More Background:
I want to write a simulation game with many interdepended values. I
don't want to create a class with dozens of functions, but let Python
create/evaluate them.

I hope this can be understood ;)

More information about the Python-list mailing list