RFC -- Hygienic Macros in Python

Jason Orendorff jason at jorendorff.com
Sat Feb 9 00:04:57 EST 2002


phil hunt wrote:
> Python macros should, if they exist, have a superset of the 
> capabilities of
> C macros. Including the ability to be replaced by arbitrary code in the
> program. Why arbitrary? firstly, because it is conceptually simple;
> secondly because it doesn't restrict the programmer.

C macros are quite restrictive, precisely *because* they simply
paste tokens into your program.

> Consider this code:
>
> defmacro incj:
>    j = j + 1

Well, I don't think that's particularly realistic code.  Consider
this instead.

Jason Orendorff wrote:
>   defmacro synchronized(lock_expr, code_block):
>       lock = ^lock_expr
>       lock.acquire()
>       try:
>           ^code_block
>       finally:
>           lock.release()

Under a non-hygienic macro system, obviously I wouldn't write it
like that because "lock" might clash with a local name.  Indeed if
I have nested "synchronized" statements it's guaranteed to clash.
So what should the code look like in a non-hygienic system?

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list