Python: shortcut for writing decorators which accept arguments?

后端 未结 5 543
闹比i
闹比i 2021-02-06 02:45

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(timeo

5条回答
  •  长情又很酷
    2021-02-06 03:28

    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
    

提交回复
热议问题