Decorators inside of class and decorator parameters

Michele Simionato michele.simionato at gmail.com
Sun Jan 14 03:35:27 EST 2007


Gabriel Genellina wrote:
> see this article by M. Simoniato
> http://www.phyast.pitt.edu/~micheles/python/documentation.html for a better
> way using its decorator factory.

Actually the name is Simionato ;)
I have just released version 2.0, the new thing is an update_wrapper
function similar to the one
in the standard library, but with the ability to preserve the signature
on demand. For instance

def traced(func):
   def wrapper(*args, **kw):
       print 'calling %s with args %s, %s' % (func, args, kw)
       return func(*args, **kw)
  return update_wrapper(wrapper, func, create=False)

works exactly as functools.update_wrapper (i.e. copies__doc__,
__module__,etc. from func to wrapper without
preserving the signature), whereas update_wrapper(wrapper, func,
create=True) creates a new wrapper
with the right signature before copying the attributes.

 Michele Simionato




More information about the Python-list mailing list