Skip to main content

Macros for Python: Quasiquotes, Case Classes, LINQ and more!

Project description


This is a customized version of the original MacroPy updated to work only with Python 3.5+. PyPy is untested.

As of now the original tests pass but some language features have yet to be implemented.

What follows is an ongoing update of the original documentation and therefore may be inaccurate.

MacroPy is an implementation of Macros in the Python Programming Language. MacroPy provides a mechanism for user-defined functions (macros) to perform transformations on the abstract syntax tree(AST) of Python code at module import time. This is an easy way to modify the semantics of a python program

Python like you’ve never seen before

MacroPy allows you to create constructs which are impossible to have in normal python code, such as:


with trace:
    sum([x + 5 for x in range(3)])

# sum([x + 5 for x in range(3)])
# range(3) -> [0, 1, 2]
# x + 5 -> 5
# x + 5 -> 6
# x + 5 -> 7
# [x + 5 for x in range(3)] -> [5, 6, 7]
# sum([x + 5 for x in range(3)]) -> 18

Quick Lambdas

print map(f[_[0]], ['omg', 'wtf', 'bbq'])
# ['o', 'w', 'b']

print reduce(f[_ + _], ['omg', 'wtf', 'bbq'])
# 'omgwtfbbq

Case Classes

class Point(x, y): pass

p = Point(1, 2)

print str(p)    #Point(1, 2)
print p.x       #1
print p.y       #2
print Point(1, 2) == Point(1, 2) # True

And more! All this runs perfectly on vanilla Python 3.5+ (an maybe on pypy, untested). For more details, see the GitHub page. or ask in the Gitter channel or the Google group .


1.0.4.dev2 (2017-09-08)

  • Add;

1.0.4.dev1 (2017-09-08)

  • Updated the import machinery and macro detection to be compatible with Python 3.5+.
  • Removed support for Python 2, supporting it would require a way to manage differences at the ast level, but i don’t use Python2 anymore.
  • Added support for Python 3.5+ in the form of new call arguments form and new AsyncFunctionDef.
  • Basic scope analysis now available in the form of the @Scoped decorator, to be used in conjunction with @Walker.


  • Error messages are now raised at run-time rather than at import time, with other improvements (double stack traces, catchability).
  • @enum macro now has much better error messages
  • Improved error messages for mis-use of stub functions outside their related macro (e.g. the u, name, ast stubs for the q/hq macros)
  • Improved error messages for invalid case class signatures
  • Hygienic Quasiquotes now allow lexical capture of module objects


  • Removed unit test from PyPI distribution


  • Fixed a bug in ast_ctx_fixer
  • gen_sym() is now gen_sym(name="sym"), allowing you to override the base name
  • Implemented macropy.case_classes.enum macro
  • Implemented macropy.quick_lambda.lazy and macropy.quick_lambda.interned macros

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for macropy3, version 1.0.4.dev2
Filename, size File type Python version Upload date Hashes
Filename, size macropy3-1.0.4.dev2.tar.gz (86.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page