No macros in Python

Mike Meyer mwm at mired.org
Mon Dec 16 00:32:37 EST 2002


Lulu of the Lotus-Eaters <mertz at gnosis.cx> writes:

> Mike Meyer <mwm at mired.org> wrote previously:
> |A proper macro facility in python should let me write:
> |        with_output_to_file "filename":
> |            code that will be executed
> |            with print statement output
> |            going into filename
> |        code that will have
> |        print statement output
> |        going to where ever it was
> |        going before the macro
> 
> You mean like:
> 
>   orig = sys.stdout               # or just use sys.__stdout__ later
>   sys.stdout = open('fname','w')  # w/ output to...
>   print this, that, other
>   sys.stdout = orig               # or sys.stdout = sys.__stdout__
>   print original, output, destination
> 
> I know it's not -exactly- the same thing... the Python version is
> shorter and clearer.

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       

which is both shorter and clearer than your "pure" python.

> |It should also allow me to add conditional expressions to the
> |language, ala the C ?: operator.
> I'm not necessarily opposed to a ternary operator.  In fact, I'd
> probably like one.  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?

We already have those binary operators, and the world hasn't
ended. Adding ternary versions won't be much worse.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list