Macros in Python? (PEP 310)

Dominic oblivious at web.de
Sat Apr 12 13:50:33 EDT 2003


> 
> def wrapfunc(func, pre=mkprint("hi"), post=mkprint("bye")):
>     def wrapped(*args, **kwds):
>         pre()
>         try: return func(*args, **kwds)
>         finally: post()
>     return wrapped

Ups, I am surprised that wrapped still has access
to func I thought that this would only work
with Python's lambda. My fault.
The pre/post calls are only executed once
when the file is loaded so this should be
handled with care...
However I do not need to pass pre/post as
parameters, so it does not matter very much.

The only drawback is that the code needs
to be separated into a fuction unless the
generator "hack" is used. So wrapfunc seems
to be the optimum so far.

Ciao,
  Dominic






More information about the Python-list mailing list