Propagating arguments to decorator which combines other decorators
<p>I have a scenario like:</p>

<pre><code>@decorator_one(1)
@foo
@bar
def my_decorated_func():
    pass
</code></pre>

<p>I am trying to condense this into something like:</p>

<pre><code>@my_custom_decorator(1)
def my_decorated_func():
    pass
</code></pre>

<p>This is straightforward if I have decorators that do not have <code>(1)</code> option:</p>

<pre><code>def my_custom_decorator(f):
    @decorator_one
    @foo
    @bar
    def wrapped():
        pass
    return wrapped
</code></pre>

<p>However I am uncertain how to properly propagate the argument to the first wrapper. </p>

<p>In this case, I am comfortable assuming that if I pass 1 to my_custom_decorator that it will always and only be the arg for decorator_one.</p>

<p>Yes, you need a decorator factory.</p>

<p>I found the solution using a class more attractive because it separates the factory and the decorator:</p>

<pre><code>class my_custom_decorator(object):

    __init__(self, *args, **kwargs):  # the factory
        self.args = args
        self.kwargs = kwargs

    __call__(self, f):  # the decorator
        @decorator_one(*self.args, **self.kwargs)
        @foo
        @bar
        def wrapped():
            pass

        return wrapped
</code></pre>

python decorator python-decorators