Using python decorator with or without parentheses

后端 未结 4 1834
心在旅途
心在旅途 2021-02-01 01:04

In Python, what is the difference between using the same decorator with and without parentheses?

For example:

Without parentheses:

@some         


        
4条回答
  •  独厮守ぢ
    2021-02-01 01:48

    some_decorator in the first code snippet is a regular decorator:

    @some_decorator
    def some_method():
        pass
    

    is equivalent to

    some_method = some_decorator(some_method)
    

    On the other hand, some_decorator in the second code snippet is a callable that returns a decorator:

    @some_decorator()
    def some_method():
        pass
    

    is equivalent to

    some_method = some_decorator()(some_method)
    

    As pointed out by Duncan in comments, some decorators are designed to work both ways. Here's a pretty basic implementation of such decorator:

    def some_decorator(arg=None):
        def decorator(func):
            def wrapper(*a, **ka):
                return func(*a, **ka)
            return wrapper
    
        if callable(arg):
            return decorator(arg) # return 'wrapper'
        else:
            return decorator # ... or 'decorator'
    

    pytest.fixture is a more complex example.

提交回复
热议问题