Macros in Python? (PEP 310)

A. Lloyd Flanagan alloydflanagan at attbi.com
Fri Apr 11 19:46:50 EDT 2003


Dominic <oblivious at web.de> wrote in message news:<b771jb$ac6$03$1 at news.t-online.com>...
> Dominic wrote:
> > Beni Cherniavsky wrote:
> > 
> >> Dominic wrote on 2003-04-09:
> >>
> >>
> >>> (defmacro with-root-privs (() &body body)
> >>>   (let ((oldidsym (gensym)))
> >>>     `(let ((,oldidsym (geteuid)))
> >>>        (seteuid 0)
> >>>        (unwind-protect
> >>>        (progn , at body)
> >>>      (seteuid ,oldidsym)))))
> >>>
> >>> Is there an easy way to produce similar code
> >>> in Python which gurantees that certain things
> >>> happen around some code? (Maybe by using
> >>> generators?)
> >>>
> >>
> ...

I think you are making this a lot harder than it needs to be. 
Remember that function definitions are first class objects in python:

def y(a, b):
    " a simple function to do nothing significant
    return a + b

_y = y  #save defn. of y

def x(a, b):
    # function to wrap stuff around y
    print "hi"
    c = _y(a, b)
    print "bye"
    return c

y = x  #rebind y so it looks identical to caller

>>> y(2, 3)
hi
bye
5

If this isn't what you are trying to do, could you clarify what's
missing?




More information about the Python-list mailing list