[Python-Dev] Recent experience with the _ast module

Collin Winter collinw at gmail.com
Tue Feb 13 01:27:06 CET 2007

I've been working this past week at converting the stdlib's compiler
package to use 2.5's new _ast module instead of the package's own AST.
Overall, _ast was a joy to work with, save for the following nitpicks:

1) There are times when the _fields attribute on some AST nodes is
None; if this was done to indicate that a given node has no
AST-related attributes, it would be much easier if _fields was [] in
this case. As it is, I had to special-case "node._fields is None" in
the visitor so that I don't accidentally iterate over it.

2) {BinOp,AugAssign,BoolOp,etc}.op is an instance of, eg, Add, Sub,
Mult, Mod, meaning you have to dispatch based on tests like
"isinstance(node.op, x)" or "type(node.op) is x". I would much, much
prefer to spell this "node.op is x", ie, use "node.op = Add" rather
than the current "node.op = Add()" when constructing the nodes.

3) I'd like there to be an Else() node for If.orelse, While.orelse,
etc. My motivation is that I need the lineno and col_offset values of
the "else" statement for a code-coverage utility; as it is, I have to
find the last lineno of the if/while/etc suite and the first lineno of
the "else" suite and then search between those two for the "else"


Collin Winter

More information about the Python-Dev mailing list