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>You could do something like that:</p>

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

def double(f):
    def wrapped(*args, **kwargs):
        return f(*args, **kwargs)
    return wrapped

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

<p>It's because we can decorate functions and run functions using a decorator function explicit as in the example above.
So in this one:</p>

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

<p>In the place of <code>a</code> you can put any function and in place of the <code>2</code>, args and kwargs.</p>

python wrapper decorator