[Python-Dev] Optimization of Python ASTs: How should we deal with constant values?

"Martin v. Löwis" martin at v.loewis.de
Wed Apr 30 22:36:36 CEST 2008

> This leaves us with a few options:

5. Reuse/Abuse Num(object) for arbitrary constants.
   AFAICT, this should work out of the box.

> 1. A new AST expr node for constant values for types other than Str/Num
> I imagine this to be something like Const(PyObject* v), which is
> effectively translated to a "LOAD_CONST v" by the compiler. This trades
> the purity of the AST for a little practicality. A "Const" node has no
> real source representation, it would exist solely for the purpose of
> injecting PyObject constants into the AST.

I think this is the way to go. It doesn't violate purity: it is an
*abstract* syntax, meaning that there doesn't need to be a 1:1
relationship to source syntax. However, it is still possible to
reproduce source code from this Const node.

I also don't worry about Jython conflicts. The grammar has a version
number precisely so that you can refer to a specific version if you
need to.


More information about the Python-Dev mailing list