[Python-ideas] History on proposals for Macros?

Chris Angelico rosuav at gmail.com
Sun Mar 29 04:13:41 CEST 2015


On Sun, Mar 29, 2015 at 12:51 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> But we might be able to rescue this proposal by dropping the requirement
> that the compiler knows when to pass the syntax tree and when to
> evaluate it. Suppose instead we had a lightweight syntax for generating
> the AST plus grabbing the current context:
>
>     x = 23
>     spam(x + 1, !(x+1))  #  macro syntax !( ... )
>
>
> Now the programmer is responsible for deciding when to use an AST and
> when to evaluate it, not the compiler, and "macros" become regular
> functions which just happen to expect an AST as their argument.

This is actually a lot more plausible than most of the other theories
(except the preprocessor, but anyone can do that, and it's not
necessarily going to help you any). If the magic macro operator is
given precedence equivalent to lambda, it would often be possible to
do it without the parens, too. In the same way that "lambda: expr"
yields an object (a function) rather than evaluating its argument, the
macro syntax would yield an object (an AST tree or somesuch) without
actually evaluating the expression.

ChrisA


More information about the Python-ideas mailing list