views:

120

answers:

1

Cody has been building a Pythonic Macro Syntax. He says

These macros allow you to define completely custom syntax, from new constructs to new operators. There's no facility for doing this in Python as it stands.

I am not sure what he means by

  • new constructs to new operators: Does he prefer to binary operators such as +, - and multiplication in Math?
  • his main goal: Where do you benefit in customizing Python's macro syntax?
+6  A: 

No doubt Cody refers to completely new operators that are not currently in Python, such as (I dunno) ^^ or ++ or +* and so on, whatever they might mean. And he's explicitly saying that the macro system lets you define a completely new syntax for Python (his question was about the syntax of the macro definitions themselves).

Some people care burningly about syntax and for example would much prefer to see Python uses braces rather than group by indentation; but Python itself will never follow those people's preferences...:

>>> from __future__ import braces
  File "<stdin>", line 1
SyntaxError: not a chance

So these people might obtain what they crave by defining a completely new syntax for Python through this macro system.

Others might use it to define specific custom languages that mostly follow Python's general outlines but add special new keywords, let you call functions without using parentheses, and so on, and so forth.

Whether that's a good thing, in fact, is an ancient, moot issue—but some languages such as Lisp have always had macros of such power, and many people who came to Python from Lisp, such as Peter Norvig, would probably be quite happy to get back that syntax-making power they used to have in Lisp but lack in Python.

Alex Martelli
@Alex: Thank you for your explanations!
Masi