[Python-3000] AST access (WAS: Adaptation vs. Generic Functions)

Steven Bethard steven.bethard at gmail.com
Sun Apr 9 20:24:00 CEST 2006


On 4/8/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> To me an AST access feature would only be useful for changing the "VM" that
> Python code runs on.  For example, there is an object-relational mapper out
> there (I think it's Robert Brewer's DejaVu system) that takes Python
> generator expressions and converts them into SQL.  Right now it does it by
> bytecode hacking, because the only alternative is to parse a string.

I can see how reading the AST could be useful here, but Python 2.5
already has that:

>>> expr = compile('(x**y for x, y in z)', '<string>', 'eval', 0x400)
>>> expr.body.elt.left.id
'x'
>>> expr.body.elt.right.id
'y'
>>> expr.body.elt.op
<_ast.Pow object at 0x00BD0690>
>>> [name.id for name in expr.body.generators[0].target.elts]
['x', 'y']
>>> expr.body.generators[0].iter.id
'z'

Do the use-cases here really need to change the AST on the original
function?  What's wrong with creating a new function?

STeVe
--
Grammar am for people who can't think for myself.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list