RFC -- HYGIENIC MACROS IN PYTHON

phil hunt philh at comuno.freeserve.co.uk
Fri Feb 8 08:18:22 EST 2002


On Fri, 08 Feb 2002 01:42:51 GMT, Courageous <jkraska at san.rr.com> wrote:
>
>>It's just that being forced -- "for my own good" -- not to use certain
>>features because someone has decreed them intrinsically bad, doesn't
>>appeal to me at all.
>
>Sure. You realize that a hygienic macro doesn't prevent you from
>modifying things by consequence, right? All it does is require for
>you to mean it; this caters to Python's phenonomenon of least
>surprise. Imagine the following
>
>>>> i = 3
>>>> j = 4
>>>> mymacro i
>>>> print j
>5
>
>That would vex some folks.

It would vex me in someone else's code unless there was a good reason for
it and it was well documented.

> But that's exactly what a non hygienic
>macro can do.

And it's exactly the behaviour I want a macro to be able to do.

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.

To take your example above, the ability to increment a global variable is 
useful because it can be used to determine how many times a macro has 
been run.

> Granted, in some cases non hygeniec macros can be
>quite useful. And the design I'm targeting is only _pseudo_ hygienic
>in any case. It's hygienic for the local namespace. 

In which case, it will behave complicatedly. Consider this code:

defmacro incj:
   j = j + 1

j = 4
print j
incj
print j

def myfun(j):
   print j
   incj
   print j

myfun(100)


In a non-hygenic macro system it is easy to see what underlying Python
code gets produced. In your system it isn't -- I've no idea what you
would want this to do -- therefore it is more complex. As a general rule,
whenever a program has more complexity than necessary, it is too complex. 
Complexity is bad: it leads to poor understanding and extra code 
therefore extra bugs. I would estimate over 90% of program bugs are
caused by not following this rule.


-- 
===== Philip Hunt ===== philh at comuno.freeserve.co.uk =====
Herbivore, a zero-effort email encryption system. Details at:
<http://www.vision25.demon.co.uk/oss/herbivore/intro.html>






More information about the Python-list mailing list