RFC -- Hygienic Macros in Python

Courageous jkraska at san.rr.com
Sun Feb 10 13:15:45 EST 2002


>>Well, I don't think that's particularly realistic code. 
>
>Nor do I, particularly. I can't offhand think of anything where I'd
>particularly want to use macros in Python, except perhaps to define my
>own control structures.

A toy example or referring to control structures are fine examples.

One doesn't have to have a useful example per se; first-class macro
systems are usually used by those who wish to craft a domain-specific
dialect of a language because in so doing they will be able to more
easily and succinctly represent their problems in the language they
are working in. This is a somewhat esoteric application; it's one
of the reasons that many AI researchers continue to use Lisp, Scheme,
et al.

> local lock
> Where the "local lock" declaration means something like: in the
> current namespace create a local variable name that doesn't clash with
> anything else.

That's a good idea that I'll think about. Surely it's easy enough
syntactically, in particular because the dialect of Python that I'm
creating has optional declarations (to support strict functions and
strict classes).

If a macro weren't hygienic by default in my system, I'd probably do
something like you suggest. Another alternative would be to require
a first-class closure form and use that when you wanted hygienic
behavior, ala:

defmacro incf3(j):
	closure(j):
		i=3
		j=j+i

In this syntax, i is created in the protected closure namespace, but
j (as indicated by the parameter to the block closure) is promulgated
_through_ into the closure, and moved by reference. This continues to
behave cleanly.

I could, I suppose, also invert it, and allow blocks of unhygienic space
with a block keyword of some kind.

C//




More information about the Python-list mailing list