[Python-Dev] Meta coding in Python

Albert Zeyer albzey at googlemail.com
Fri Sep 16 00:44:35 CEST 2011


Hi list,

I thought it would be nice in Python to allow some sort of meta coding
(which goes far ahead of simple function descriptors).

The most straight forward way would be to allow operations on the AST.

I wrote a small patch for CPython 2.7.1 which, for each code object,
adds the related AST of the statement to a new attribute `co_ast`.

https://github.com/albertz/CPython/commit/2670e621458fd80311fc02897b698ea2a36d494b

Some simple demonstration of what you can do with this:

https://github.com/albertz/CPython/blob/astcompile_patch/test_co_ast.py

I'm not sure wether the Python AST in this form is optimal for doing
such things, though. Maybe another representation would be more
efficient and result in simpler code for doing transformations.

Discussion about this is very welcome.

Regards,
Albert


More information about the Python-Dev mailing list