No macros in Python

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


|Lulu of the Lotus-Eaters <mertz at gnosis.cx> writes:
|>   sys.stdout = open('fname','w')  # w/ output to...
|>   print this, that, other
|>   sys.stdout = sys.__stdout__
|>   print original, output, destination

Mike Meyer <mwm at mired.org> wrote previously:
|The only reason the python version is short is because you used real
|code instead of long-winded comments. Done with your code, the macro
|version looks like this:
|        with_output_to_file "fname": print this, that, other
|        print original, output, destination

Well sure.  As far as golf goes, you can make a support function to make
the function look like:

    from not_a_macro import with_output_to_file
    with_output_to_file(fname, statement-list)
    print original, output, destination

The function can be implemented along the lines of:

    def with_output_to_file(fname, stmts):
        import sys
        orig = sys.stdout
        sys.stdout = open(fname,'w')
        for stmt in stmts: exec stmt
        sys.stdout = orig

I don't know exactly how many characters might be in the hypothetical
macro definition, but Meyer's version really doesn't seem to get you
ANYTHING other than using a colon instead of parentheses.

Actually, if you really hate parents, you can use:

    print >> file, this, that, other

But I don't recommend that :-).

|> But I am opposed down to my bones to a ternary
|> operator defined by an individual programmer in whatever idiosyncratic,
|> perhaps inadequately tested, fashion she chooses.

|As opposed to having binary operators defined by an individual
|programmer in whatever idiosyncratic, perhaps inadequately tested
|fashion she chooses?

But binary operators are VERY different from the ternary one.  You can't
change the meaning of "+" between two integers.  You can only change the
meaning where at least one of the objects is a custom type.

This difference is much more profound that just the difference between
two and three expressions involved.  The symbol "+" -inherently- means
something different when it operates on different datatypes.  String
concatenation is different from integer arithmetic, for example.
ANYTHING a custom object does when the "+" operator is applied is
"special"... when you create a class, you have to decide what (if
anything) it means to add it to somethign else.

The difference here is that it is the OBJECTS, not the operator, that is
changed.  Macros change the operator itself, which I find disturbing.

This works even if there were a ternary operator, assuming I could write
in Python 3.0:

    foo = pred ? this : that

I would pretty much expect the meaning of evaluating the truth value of
'pred' to depend on its .__nonzero__() method, or some similar magic
method.  How 'pred' behaves in a boolean context depends on the type of
object it is... which is how things should be.

Yours, Lulu...

--
 mertz@  _/_/_/_/ THIS MESSAGE WAS BROUGHT TO YOU BY: \_\_\_\_    n o
gnosis  _/_/             Postmodern Enterprises            \_\_
.cx    _/_/                                                 \_\_  d o
      _/_/_/ IN A WORLD W/O WALLS, THERE WOULD BE NO GATES \_\_\_ z e





More information about the Python-list mailing list