How to use Python Decorators_0

  1. Authorization
  2. Logging

通過裝飾器可以來打印日誌:

from functools import wraps


def logit(func):
    @wraps(func)
    def with_logging(*args, **kwargs):
        print(func.__name__ + " was called")
        return func(*args, **kwargs)

    return with_logging


@logit
def addition_func(x):
    """Do some math."""
    return x + x

result = addition_func(4)
# Output: addition_func was called

最後的回顯是:

addition_func was called

需要注意下Decorators的return順序;

Decorators自身不會Return;

最後Return的是Function_Operation

最先Return的是Function

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章