[Python-Dev] AST optimizer implemented in Python

Mark Shannon mark at hotpy.org
Mon Aug 13 10:34:10 CEST 2012

Brett Cannon wrote:
> On Sat, Aug 11, 2012 at 8:16 PM, Eric Snow <ericsnowcurrently at gmail.com 
> <mailto:ericsnowcurrently at gmail.com>> wrote:
>     On Sat, Aug 11, 2012 at 6:03 PM, Brett Cannon <brett at python.org
>     <mailto:brett at python.org>> wrote:
>      > It would also be very easy to expand importlib.abc.SourceLoader
>     to add a
>      > method which is called with source and returns the bytecode to be
>     written
>      > out
>     Yes, please.  Not having to hack around this would be nice.
> http://bugs.python.org/issue15627

AST transformation is a lot more general than just optimization.

Adding an AST transformation is a relatively painless way to add to
Python the last element of lisp-ness that it lacks:
Namely being able to treat code as data and transform it at runtime,
after parsing but before execution.

Some examples:
Profiling code be added by an AST transformation.
IMO this would have been a more elegant way to implement CProfile
and similar profilers than the current approach.

AST transformations allow DSLs to be implemented in Python
(I don't know if that is a + or - ).

Access to the AST of a function at runtime would also be of use to
method-based dynamic optimizers, or dynamic de-optimizers for static

All for the price of adding a single method to SourceLoader.
What a bargain :)


More information about the Python-Dev mailing list