No macros in Python

Mike Meyer mwm at mired.org
Mon Dec 16 00:51:52 EST 2002


Peter Hansen <peter at engcorp.com> writes:
> > I'm sure Mike meant "lisp-like macros", not C macros.  Not being a lisper,
> > though, I can't say why those would be so indispensible, either. 
> I read his response, which confirms what you suggest, but I still have not
> grasped the *value* of doing this.  I don't seem to encounter anything 
> while writing Python code which makes me think grumblingly to myself "dang,
> if I had a macro facility this would be a lot easier", so I'm still unsure
> what the value would be.

I've only wished for macros once when writing Python. On the other
hand, what I was doing was very complicated, and I'm not sure anything
could have rescued it.

> Mike's examples all involve changing the language's syntax (that's what
> I gather from what I understood, anyway)

Real macros change the language more fundamentally than that, by
changing what is understood to be the syntax of the language. In
python, keyword terminated by a ":" are part of the syntax. In the
hypothetical macro version of python, that's changed.  Macros can now
fill that role. From one viewpoint, the macro adds syntax. From
another, it doesn't.

> which is widely considered to be a Bad Thing, but he attempts to
> justify it by comparison with redefining operators.

Actually, I'm trying to refute the argument that being able to write
bad macros makes macros a bad thing.

> The problem is, redefining operators is also a
> Bad Thing unless you keep the meaning identical to or *very* close to 
> the original, so the justification falls apart, in my opinion.  

So you agree there are good uses for redefined operators. There are
good uses for macros as well. You can also do Bad Things with
functions, or even an if statement.

> If the only good macro is one which doesn't change the language syntax, 
> then you don't really need the macro, goes my logic...

Macros are a general-purpose mechanism. There are lots of things they
let you do. Lazy evaluation, infinite lists, CLOS, short-circuit
booleans, and so on. Some of these things are already in Python. Some
are part of proposed future changes. Some may never be there at
all. All the proposed changes are changing the interpreter and the
syntax of the language to add a feature that could be implemented *in
the language* if it had macros.

Now, would you rather have one general-purpose mechanism that makes a
lot of things possible, or a lot of special-purpose mechanisms?

        <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