Decorating functions without losing their signatures

Rotwang sg552 at hotmail.co.uk
Wed Apr 3 21:53:30 EDT 2013


On 03/04/2013 05:15, Steven D'Aprano wrote:
> 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!

Thanks. It'll take me a while to fully absorb the links, but it looks 
like both are similarly based on abusing the exec function.

Thanks to Jan too.



More information about the Python-list mailing list