9

Does the Python standard library have a shortcut for writing decorators which accept arguments?

For example, if I want to write a decorator like with_timeout(timeout):

@with_timeout(10.0)
def cook_eggs(eggs):
    while not eggs.are_done():
        eggs.cook()

I have to write something like:

def with_timeout(timeout):
    _func = [None]
    def with_timeout_helper(*args, **kwargs):
        with Timeout(timeout):
            return _func[0](*args, **kwargs)
    def with_timeout_return(f):
        return functools.wraps(f)(with_timeout_helper)
    return with_timeout_return

But that's awfully verbose. Is there a shortcut which makes decorators which accept arguments easier to write?

Note: I realize that it's also possible to use three nested functions to implement decorators with arguments… But that feels just a bit suboptimal too.

For example, possibly something like a @decorator_with_arguments function:

@decorator_with_arguments
def timeout(f, timeout):
    @functools.wraps(f)
    def timeout_helper(*args, **kwargs):
        with Timeout(timeout):
            return f(*args, **kwargs)
    return timeout_helper
1

5 Answers 5

7

I tend to write my decorators as classes to be honest

class TestWithArgs(object):
    def __init__(self, *deco_args, **deco_kwargs):
        self.deco_args = deco_args
        self.deco_kwargs = deco_kwargs
    def __call__(self, func):
        def _wrap(self, *args, **kwargs):
            print "Blah blah blah"
            return func(*args, **kwargs)
        return _wrap

Its nothing if not slightly clearer

4

I know you said it feels suboptimal but I still feel that using three nested models is the cleanest solution. The inner two functions are just the 'normal' way of defining a decorator for a function that takes arguments (see example in python's docs for @wraps). The outer one is really just a function that takes and argument and returns a decorator.

def with_timeout(timeout):
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            with Timeout(timeout):
                return f(*args, **kwargs)
        return wrapper
    return decorator
1
  • Sorry, but I disagree. There is nothing at all clean about three nested functions (or, for that matter, the way I do it). It's a hack. Better would be, for example, a @decorator_with_arguments decorator (see my updated question). May 16, 2012 at 2:30
4

Based on Jakob's suggestion, I've implemented a small Decorator class, which I feel does a fairly decent job:

class Decorator(object):
    def __call__(self, f):
        self.f = f
        return functools.wraps(f)(lambda *a, **kw: self.wrap(*a, **kw))

    def wrap(self, *args, **kwrags):
        raise NotImplemented("Subclasses of Decorator must implement 'wrap'")

class with_timeout(Decorator):
    def __init__(self, timeout):
        self.timeout = timeout

    def wrap(self, *args, **kwargs):
        with Timeout(timeout):
            return self.f(*args, **kwargs)
2
  • 1
    I fixed a typo on your with_timeout method warp May 16, 2012 at 5:40
  • D'oh! Thanks. That's probably the 8th time I've typed 'warp' instead of 'wrap' tonight. And I haven't typed 'wrap' that many times. May 16, 2012 at 5:55
0

First, we can define a little meta-decorator:

def decorator_with_arguments(wrapper):
    return lambda *args, **kwargs: lambda func: wrapper(func, *args, **kwargs)

That allows us to create decorators that accept arguments like so:

@decorator_with_arguments
def my_wrapper(func, *decorator_args, **decorator_kwargs):
    def wrapped(*call_args, **call_kwargs):
        print "from decorator:", decorator_args, decorator_kwargs
        func(*call_args, **call_kwargs)
    return wrapped

Which can then be used normally:

@my_wrapper(1, 2, 3)
def test(*args, **kwargs):
    print "passed directly:", args, kwargs

test(4, 5, 6)

Adding functools.wraps decoration is left as an exercise :)

0

Another take, without using lambdas:

def decorator_with_arguments(f):
    @functools.wraps(f)
    def with_arguments_helper(*args, **kwargs):
        def decorator(g):
            return f(g, *args, **kwargs)
        return decorator
    return with_arguments_helper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.