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): return x + x result = addition_func(4)
1. 遇到@logit , 开始执行 logit 函数,并把 “addition_func”作为参数去代替 “func”
2. 把 “additon_func" 替换为 with_logging 函数
3 . 把 "with_logging "函数读入内存中。
4 . 执行 result = addition_func(4)
这时候执行 with_logging (4)
print(func.__name__ + " was called") ===> " addition_func was called"
retrun func(*args,**kwargs) ====> additon_func(4)=8 ===>result