No macros in Python

Patrick W patrickw106 at yahoo.com.au
Tue Dec 17 23:15:20 EST 2002


Peter Hansen <peter at engcorp.com> writes:

> [ Section on macros in Paul Graham's "On Lisp" ]

These comments about utility functions (Paul Graham, On Lisp, pages
59-60) are also applicable to macros, to a great extent:

"If your code uses a lot of new utilities, some readers may complain
that it is hard to understand. People who are not yet very fluent in
Lisp will only be used to reading raw Lisp. In fact, they may not be
used to the idea of an extensible language at all. When they look at a
program which depends heavily on utilities, it may seem to them that
the author has, out of pure eccentricity, decided to write the program
in some sort of private language.

"All these new operators, it might be argued, make the program harder
to read. One has to understand them all before being able to read the
program. To see why this kind of statement is mistaken, consider the
case described on page 41, in which we want to find the nearest
bookshops. If you wrote the program using find2, someone could
complain that they had to understand the definition of this new
utility before they could read your program. Well, suppose you hadn t
used find2. Then, instead of having to understand the definition of
find2, the reader would have had to understand the definition of
find-books, in which the function of find2 is mixed up with the
specific task of finding bookshops. It is no more difficult to
understand find2 than find-books. And here we have only used the new
utility once. Utilities are meant to be used repeatedly. In a real
program, it might be a choice between having to understand find2, and
having to understand three or four specialized search routines. Surely
the former is easier.

"So yes, reading a bottom-up program requires one to understand all
the new operators defined by the author. But this will nearly always
be less work than having to understand all the code that would have
been required without them. 

"If people complain that using utilities makes your code hard to read,
they probably don t realize what the code would look like if you
hadn't used them. Bottom-up programming makes what would otherwise be
a large program look like a small, simple one. This can give the
impression that the program doesn't do much, and should therefore be
easy to read. When inexperienced readers look closer and find that
this isn t so, they react with dismay. 

"We find the same phenomenon in other fields: a well-designed machine
may have fewer parts, and yet look more complicated, because it is
packed into a smaller space. Bottom-up programs are conceptually
denser. It may take an effort to read them, but not as much as it
would take if they hadn t been written that way. 

"There is one case in which you might deliberately avoid using
utilities: if you had to write a small program to be distributed
independently of the rest of your code. A utility usually pays for
itself after two or three uses, but in a small program, a utility
might not be used enough to justify including it."

<end quote>

Incidentally, the material in the first few chapters of "On Lisp" (
http://www.paulgraham.com/onlisp.html ) would, I think, be of interest
to some Pythonistas.  "Bottom up" programming works extremely well in
Python too.



More information about the Python-list mailing list