[Python-Dev] decorator support

"Martin v. Löwis" martin at v.loewis.de
Sun Sep 5 11:16:45 CEST 2004


Raymond Hettinger wrote:
> So, it would be nice if there were some support for carrying forward the
> argspec to inform help(), calltips(), and inspect().

What were you thinking of? I could imagine a predefined class, such as

class copyfuncattrs:
     def __init__(self, f):
         self.f = f

     def __call__(self, func):
         res = self.f(func)
         res.__name__ = func.__name__
         res.__doc__ = func.__doc__
         res.__dict__.update(func.__dict__)
         return res

This could be used to define a decorator

@copyfuncattrs
def trace(f):
     def do_trace(*args):
         print "invoking", f.__name__, args
         return f(*args)
     return do_trace

which in turn could be used to decorate a function

@trace
def hello():
     "Print a nice greeting"
     print "Hello, world"

which in turn could be called and inspected

hello()
print hello.__doc__

Then, the question is where copyfuncattrs should live, and I would
object that to be yet another builtin.

Regards,
Martin


More information about the Python-Dev mailing list