No macros in Python

Lulu of the Lotus-Eaters mertz at gnosis.cx
Mon Dec 16 17:25:41 EST 2002


Mike Meyer <mwm at mired.org> wrote previously:
|Ok.  You hand it an arbitrary set of python statements and a
|string. Those statements are executed with all output that would
|normally go to sys.stdout appended to the file. That output having
|been appended to the file is the only change in the i/o subsystem
|after the code has executed...

I'm not actually going to bother doing this, but we all realize this is
simple enough to do with a Python function.  It's not a function I
particularly need, but my earlier example demonstrated the general style
for writing the specified function.  Do one to your satisfaction, then
stick it in a library for later use.

|Macros don't change the syntax of the language any more than functions
|- or callable objects in general - do.

The expression:

    with_output_to_file('fname',['print x,y,z'])

matches the production:

    call ::= primary "(" [argument_list [","]] ")"

in <http://www.python.org/doc/current/ref/grammar.txt>

Show me which production matches:

    with_output_to_file "fname" : print x,y,z

If you cannot, your macro changes the syntax.  It's as simple as that.

|Another prettier way is:
|        val = (foo, bar)[pred]
|but it evaluates both foo and bar regardless of pred.

In other words, this is not semantically a ternary operator.  At least
not the one that C programmers are familiar with.  But that spelling
does have a certain elegance where the extra evaluation doesn't matter.

Yours, Lulu...

--
    _/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: Postmodern Enterprises _/_/_/
   _/_/    ~~~~~~~~~~~~~~~~~~~~[mertz at gnosis.cx]~~~~~~~~~~~~~~~~~~~~~  _/_/
  _/_/  The opinions expressed here must be those of my employer...   _/_/
 _/_/_/_/_/_/_/_/_/_/ Surely you don't think that *I* believe them!  _/_/





More information about the Python-list mailing list