Decorating functions without losing their signatures

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 3 00:15:05 EDT 2013


On Wed, 03 Apr 2013 02:05:31 +0100, Rotwang wrote:

> Hi all,
> 
> Here's a Python problem I've come up against and my crappy solution.
> Hopefully someone here can suggest something better. I want to decorate
> a bunch of functions with different signatures;
[...]
> After thinking about it for a while I've come up with the following
> abomination:
[...]
> It seems to work, but I don't like it. Does anyone know of a better way
> of doing the same thing?


Wait until Python 3.4 or 3.5 (or Python 4000?) when functools.wraps 
automatically preserves the function signature?

Alas, I think this is a hard problem to solve with current Python. You 
might like to compare your solution with that of Michele Simionato's 
"decorator" module:

http://micheles.googlecode.com/hg/decorator/documentation.html


See this for some other ideas:

http://numericalrecipes.wordpress.com/2009/05/25/signature-preserving-
function-decorators/



Good luck!




-- 
Steven



More information about the Python-list mailing list