Python decorator logger
<p>I have the following code:</p>

<pre><code>def log(func):
    def wrapper(*args, **kwargs):
        func_str = func.__name__
        args_str = ', '.join(args)
        kwargs_str = ', '.join([':'.join([str(j) for j in i]) for i in kwargs.iteritems()])
        with open('log.txt', 'w') as f:
            f.write(func_str)
            f.write(args_str)
            f.write(kwargs_str)
        return func(*args, **kwargs)
    return wrapper()


@log
def example(a, b):
    print('example')
</code></pre>

<p>However, even without calling any function, I still get the error: </p>

<pre><code>TypeError: example() takes exactly 2 arguments (0 given)
</code></pre>

<p>Can someone explain to me why this happens, because it seems the function is called, but I don't understand why.</p>

<p>You should return the <code>wrapper</code> function without calling it:</p>

<pre><code>return wrapper
</code></pre>

<p>Calling it implies the call to <code>wrapper</code> has to be evaluated, which you're however calling with the wrong signature.</p>

python decorator