Skip to the content.

G+: For the last couple of weeks I've been …

David Coles
For the last couple of weeks I've been interested in Python AST rewriting and some of the interesting possibilities it offers.

Python doesn't have syntactic macros or blocks, thus it's not really possible to directly redefine the language but with a combination of PEP-302 Import Hooks and the ast module you can do some pretty gnarly things.

- py.test (http://pytest.org) rewrites `assert` statements into function calls to allow better assert inspection in tests.
- hy (http://hylang.org) implements a new lisp dialect on top of Python's AST (allowing full interop and importing between .hy and .py files)
- macropy (https://github.com/lihaoyi/macropy) implements a full syntactic macro system that's run at import time.

#Python #AST

Behind the scenes of py.test's new assertion rewriting


(+1's) 1