No macros in Python

Peter Hansen peter at engcorp.com
Tue Dec 17 21:10:47 EST 2002


Miki Tebeka wrote:
> 
> Peter Hansen wrote:
> > In what way would macros (as I understand them... from C for example)
> > improve Python, other than possibly a small improvement in performance?
> 
> I think reading http://www.paulgraham.com/onlisp.html helped me best
> to understand what macros can do.

Thanks for this interesting (and freely downloadable, I might add!)
reference.  It will get a rather fuller reading from me some other time. :)

I've read the section on macros, and found this point most interesting:

 --When you do need a macro, what do you need from it? Macros can do two
   things that functions can’t: they can control (or prevent) the evaluation 
   of their arguments, and they are expanded right into the calling context. 
   Any application which requires macros requires, in the end, one or both of 
   these properties.

As near as I can tell, both of these translate into loss of the explicitness
which is a cornerstone of the Python "philosophy".  Currently, reading code
and encountering something that looks like a function call, I can assume
it is a function and that (a) its arguments are all evaluated, and (b)
it has no access to the calling context.  (The latter is of course able
to be violated, but we needn't consider this situation in normal code.)

Anything that doesn't look like a function is part of the syntax of
the language and I can check the reference for how it works, in the 
unlikely event I'm not familiar with it (or if it's a new feature).

I understand the "value" of macros as proponents see them, but where I'm 
coming from is a production code environment in the corporate world.  
I can't afford to have the trickiness that comes from many of even the
*intended* uses of macros in code that has to be maintained, I believe.

I think the biggest source of a difference of opinion in this area might
stem from some of us being what I'd call "CS" types, and others who are
what I'd call "SW engineer" types (to pick two phrases I use without
intention of denigrating either type).  

And as I said, *I* haven't encountered a situation in Python where I'd
rather see people defining little macros that change the language in
a little local context than finding a different approach, preferably
a very explicit one, to accomplish what they think they need.

That doesn't mean, however, that macros wouldn't be a great thing for
someone else in a different environment... to each his/her/its own.

-Peter



More information about the Python-list mailing list