- 代码
# wraps的参数是原来的函数,修饰返回的函数
def _implicit_lookup(self, subroutine, arg_name): # feed 里面查找 animal
replacer = ArgumentReplacer(subroutine, arg_name)
@wraps(subroutine) # 换汤不换药,不认识了subroutine
def lookup_closure(*args, **kwargs):
with replacer.replace_and_call(args, kwargs) as replaced: # 【注意】不是*args, **kwargs
if self._should_be_replaced(replaced.value):
self._lookup_in_store_and_replace_argument(replaced)
return replaced.returned_value
return lookup_closure
- 对比
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction(*args, **kwargs):
print("I am doing some boring work before executing a_func()")
a_func(*args, **kwargs) # 【注意】不是 a_func(args, kwargs)
print("I am doing some boring work after executing a_func()")
return wrapTheFunction