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

posted @ 2023-12-05 17:23  MoKin_Li  阅读(2)  评论(0编辑  收藏  举报