python decorate function call
<p>I recently learned about decorators and wondered if it's possible to use them not in a function definition but in a function call, as some kind of general wrapper.</p>

<p>The reason for that is, that I want to call functions from a module through a user-defined interface that does repeatable things to a function and I don't want to implement a wrapper for every single function.</p>

<p>In principle I would like to have something like</p>

<pre><code>def a(num):
    return num

@double
a(2)
</code></pre>

<p>returning 4 without the need of having access to the implementation of <code>a</code>. 
Or would in this case a global wrapper like</p>

<pre><code>def mutiply(factor,function,*args,**kwargs):
    return factor*function(*args,*kwargs)
</code></pre>

<p>be the better choice?</p>

<p>There is a very good detailed section on decorators in Marty Alchin's book <strong>Pro Python</strong> from Apress.</p>

<p>While the new style @decorator syntax is only available to be used at function definition, you can use the older syntax, which does the same thing this way:</p>

<pre><code>from module import myfunc

myfunc = double_decorator(myfunc)

x = myfunc(2) # returns 4
</code></pre>

python wrapper decorator