[Python-Dev] A Hygienic Macro System in Python?

Daniel Mahler mahler@cyc.com
Mon, 18 Mar 2002 22:02:32 -0600


David Ascher writes:
 > Daniel Mahler:
 > 
 > > Consider a html printer macro library:
 > > 
 > > html:
 > >     header:
 > >         title:
 > >             print "blah"
 > >     body:
 > >         h1:
 > >             print "bleh"
 > >         p:
 > >             print "foo"
 > >             a (href="http://bar.org"):
 > >                 print "bar"
 > >             print "baz"
 > > 
 > 
 > funny.  That's not too far from code that I write in Quixote w/ some
 > extra functions:
 > 
 > 
 > template page(request):
 > 	header(title("blah"))
 > 	body(hl("bleh"),
 >              p(("foo", 
 >                 a(href="...","bar")),
 >              "baz"
 >  
            )
Neat!

 > note that template is the Quixote part, which removes the need for
 > unsightly prints =)

I agree that the prints are annoying,
but the idea is that the body
is a command and gets evaluated lazily.
How do you accomplish that with Quixote?

 > My vote on the notion is that I want to know what good macros people
 > would write, and then implement them in a non-macro fashion.  The lock
 > or connection examples to me indicate a possible language addition
 > which

But the problem is that even the connections in a wide variety:
* tcp
* database
* ftp
...

Thus one would at least want a generic mechanism for something like
before/after/around methods, where the body can be specified at run time.
Personally, though, I like the idea of being able to abstract control,
which you can't do with methods/procedures easily,
but is natural code generation.
Maybe the new metaclasses have something to offer here.
However the advantage of macros is that they are expanded at compile time,
and so actually increase efficiency

Probably the best references on good uses of macros, I know of,
are in the following books:
* Paul Graham: On Lisp
* Czarnecki & Eisenecker: Generative Programming

On the web there is the Blitz++ library which makes heavy use of C++ templates:
http://www.oonumerics.org/blitz/papers/
http://osl.iu.edu/~tveldhui/papers

Daniel

 > boils down to something nice and essential (in the meaning of 'of the
 > essence').  Macros in their generality scare me.
 > 
 > --david
 > 
 > _______________________________________________
 > Python-Dev mailing list
 > Python-Dev@python.org
 > http://mail.python.org/mailman/listinfo/python-dev