[Python-Dev] defmacro (was: Anonymous blocks)

Robert Brewer fumanchu at amor.org
Mon Apr 25 22:30:31 CEST 2005


Michael Chermside wrote:
> Jim:
> > Why not just introduce macros?  If the answer is "We 
> > should, it is just hard to code", then use a good
> > syntax for macros.  If the answer is "We don't want
> >       xx sss (S\<!   2k3 ]
> > to ever be meaningful", then we need to figure out exactly what to
> > prohibit.
>      [...]
> > Do we want to limit the changing part (the "anonymous block") to
> > only a single suite?  That does work well with the "yield" 
> syntax, but it
> > seems like an arbitrary restriction unless *all* we want 
> are resource
> > wrappers.
> >
> > Or do we really just want a way to say that a function 
> should share its
> > local namespace with it's caller or callee?  In that case, 
> maybe the answer
> > is a "lexical" or "same_namespace" keyword.
> 
> My own opinion is that we DO want macros. I prefer a language 
> have a few,
> powerful constructs rather than lots of specialized ones. (Yet I still
> believe that "doing different things should look 
> different"... which is
> why I prefer Python to Lisp.) I think that macros could solve a LOT of
> problems.
> 
> There are lots of things one might want to replace within macros, from
> identifiers to punctuation, but I'd be willing to live with 
> just two of them: expressions, and "series-of-statements"
> (that's almost the same as a block). There are only two places
> I'd want to be able to USE a macro: where an expression is
> called for, and where a series-of-statements is called for.
> In both cases, I'd be happy with a function-call 
> like syntax for including the macro.

By "function-call like syntax" you mean something like this?

    def safe_file(filename, body, cleanup):
        f = open(filename)
        try:
            body()
        finally:
            f.close()
            cleanup()

    ...

    defmacro body:
        for line in f:
            print line[:line.find(":")]
    
    defmacro cleanup:
        print "file closed successfully"
    
    safe_file(filename, body, cleanup)

If macros were to be evaluated at runtime, I'd certainly want to see
them be first-class (meaning able to be referenced and passed around); I
don't have much of a need for anonymous macros.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org


More information about the Python-Dev mailing list