RFC -- HYGIENIC MACROS IN PYTHON

Courageous jkraska at san.rr.com
Fri Feb 8 12:09:51 EST 2002


On Fri, 8 Feb 2002 13:18:22 +0000, philh at comuno.freeserve.co.uk (phil hunt) wrote:

>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.

>defmacro incj:
>   j = j + 1

>In a non-hygenic macro system it is easy to see what underlying Python
>code gets produced. In your system it isn't -- 

Perhaps only because it hasn't been explained well enough. The only
way the above macro definition could work without issuing an exception
is if j already exists in the global namespace. Using current python
forms, it would simply fail. That's because as a hygienic macro, it
has nothing but it's own namespace (as defined at the caller) to
manipulate, and without offering the explicit lexical closure, there's
nothing in it. This is the error you would get:

>>> j = j + 1
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    j = j + 1
NameError: name 'j' is not defined
>>> 

With the following modification, it would work as expected:

defmacro incj (j):
	j = j + 1

C//




More information about the Python-list mailing list