No macros in Python

Mike Meyer mwm at mired.org
Tue Dec 17 00:08:25 EST 2002


Lulu of the Lotus-Eaters <mertz at gnosis.cx> writes:

> Mike Meyer <mwm at mired.org> wrote previously:
> ...re. function: with_output_to_file()...
> |It can't be done, because the lack of a real macro facility means the
> |programmer interface can't be done to my satisfaction.
> Oh c'mon.  This is just childish!

But it's true.

> What it that functions are unable to do?!
> Catch exceptions?  Determine their caller's namespace?  Close open files
> explicitly?  Those are the specs you mentioned that my example didn't
> happen to do.  Now of course, if what it means to do it "to your
> satisfaction" is "invoke without parentheses" you're right (but no one
> who uses Python will ever care).

Actually, it isn't the parens that are the problem, though I'd like
them to go away. You either have to pass strings, meaning you've
complicated the users life by putting a distance between the point you
write the code and the point the compiler catches syntax errors, or
pass lambdas, which means you can't pass multi-line code blocks
without jumping through hoops.

> |As I said before, adding macros changes the syntax of the language,
> |just like adding callable objects does. A macro doesn't change the
> |syntax of python-with-macros any more than a function changes the
> |syntax of python-with-callable-objects.
> Well.. of course callable objects were not "added" to any version of
> Python, since they were there from the start.  But even for some
> hypothetical python-without-callables, macros are different.  You
> -cannot- give a formal EBNF description for the syntax of every
> expression that a macro make allowable...  I think that is necessarily
> true for a non-trivial macro system.

No, it isn't. The one I described just below this has the same BNF as
python, and what I'd call a non-trivial macro system.

> |And you'd invoke it as "evalone(pred, expression1(), expression2())".
> Yuck.

True, it's uglier than the C version. But it's prettier than any of
the python versions.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list