Changing an AST
Leif K-Brooks
eurleif at ecritters.biz
Mon Oct 10 07:19:20 EDT 2005
beza1e1 wrote:
> Is it possible compiler.parse a statement, then change and then
> execute/resolve it?
This should work:
>>> from compiler.pycodegen import ModuleCodeGenerator
>>> from compiler.misc import set_filename
>>> from compiler import parse
>>>
>>> tree = parse('foo = 42')
>>> set_filename('<foo>', tree)
>>> code = ModuleCodeGenerator(tree).getCode()
>>> exec code
>>> foo
42
Also, if you need to turn an AST back into Python code, I have some ugly
code for doing that: <http://ecritters.biz/asttosource.py>.
More information about the Python-list
mailing list