带参数的装饰器:@wraps()

 1 import time
 2 from functools import wraps
 3 
 4 def time_fun(type):
 5     def outter(func):
 6         @wraps(func)  #可以得到原始函数add的原始信息 
 7         def inner(*args, **kwargs):
 8             time_start = time.time()
 9             func(*args, **kwargs)
10             time_end = time.time()
11             print(time_end - time_start)
12         return inner
13     print(type)
14     return outter
15 
16 
17 @time_fun('ok')
18 # <function time_fun.<locals>.outter.<locals>.inner at 0x00000158C9810BF8>
19 # <function add at 0x000001B9CEBD0AE8>
20 def add(x, y):
21     time.sleep(2)
22     return x+y
23 
24 print(add)

相关文章:

  • 2021-06-22
  • 2022-12-23
  • 2022-01-31
  • 2021-05-23
  • 2022-01-07
  • 2021-06-01
  • 2021-08-30
猜你喜欢
  • 2021-08-17
  • 2022-03-06
  • 2021-10-17
  • 2021-12-19
相关资源
相似解决方案