No macros in Python

Peter Hansen peter at engcorp.com
Mon Dec 16 08:38:04 EST 2002


Mike Meyer wrote:
> 
> Peter Hansen <peter at engcorp.com> writes:
> > 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.

Actually, I don't agree there are good uses for "redefined"
operators, depending on what that means.  I believe "redefining"
operators for new data types (some class I've created) in a
way that is very compatible with the previous operator behaviour
makes sense.  In this sense, it's like defining macros which 
look exactly like the existing syntax of the language.

Redefining an operator to do something unusual and unexpected is
bad.  I think most or maybe all macros act in this manner, and 
are therefore potentially also bad.

> > 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.

And such changes are reviewed extensively (usually) and are clearly
documented and *standard* across that version of the language.  

A given macro is one programmer's bizarre creation, perhaps with
inadequate justification for doing it.  (Okay, I'm overstating the
case, but I suspect in any example one might throw at me, I would
probably look at it and say "I'd prefer to see that in a function."
Courageous is quite quite right when he writes to me "Whether or 
not _you_ value this is, of course, a separate question.")

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

I like functions, to make it clear to maintainers what I was
trying to do when they decide it was a bad idea after all and want
to improve my code.  With macros, I suspect the maintainers would
run away and my code would stay bad.

-Peter



More information about the Python-list mailing list