do...until wisdom needed...

Johann Hibschman johann at physics.berkeley.edu
Wed Apr 18 00:57:59 EDT 2001


Douglas Alan writes:

> Make your own dialect, no.  Make your own embedded language, yes.
> But libraries can act as embeded languages too.  Without the support
> of macros, however, the syntax to use them is sometimes painful.

Hi folks.  I don't want to get involved in this pissing contest, but I
thought I'd make a quick observation.  Most of the point of using a
Lisp is the macro facility, so people expect it.  For Python, there's
no such culture of syntax extension, so any effort in that direction
is a lost cause.

I've yet to see an algol-y language that does macros well; I've heard
good things about Dylan but I haven't looked into it yet.

However, I've been playing with Ruby a bit, and have found that the
concept of "blocks", which I gather they've taken from Smalltalk,
makes most uses of macros unnecessary.  Could Python grow something
like this?  Basically, it extends off the idea that if you make
lambda's easy enough to use, you don't need macros.

As an example, in Ruby I can do something like

with_directory ("/Users/johann/Documents") do
    `cvs update`
end

which would expand (in python) to the equivalent of

try:
   old_dir = os.getcwd()
   os.chdir("/Users/johann/Documents")
   os.system("cvs update")
finally:
   os.chdir(old_dir)

If Python would have something like this, most of my own personal
desires for macros would be handled.

Perhaps:

with_directory("/Users/johann/Documents"):
    os.system("cvs update")

where I've defined (in pseudo-python)

def with_directory(dir, &yield):
    old_dir = os.getcwd()
    try:
        os.chdir(dir)
        yield()           # calls the os.system block
    finally:
        os.chdir(old_dir)

where the &yield would wrap up the os.system call, etc.  That's
probably not going to happen (although I've heard some promising
things from the python-dev people), but it's a nice ability to have.

Just food for thought,

--Johann

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list