Advertisement
ClearCode

Basic decorator

May 5th, 2022
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. import time
  2.  
  3. def decorator(func):
  4.     def wrapper():
  5.         print('decoration begins')
  6.         func()
  7.         print('decoration ends')
  8.     return wrapper
  9.  
  10. def duration_decorator(func):
  11.     def wrapper():
  12.         start_time = time.time()
  13.         func()
  14.         duration = time.time() - start_time
  15.         print(f'duration {duration}')
  16.     return wrapper
  17.  
  18. def double_decorator(func):
  19.     def wrapper():
  20.         func()
  21.         func()
  22.     return wrapper
  23.  
  24. @double_decorator
  25. @decorator
  26. @duration_decorator
  27. def func():
  28.     print('Function')
  29.     time.sleep(1)
  30.  
  31. #func = decorator(func)
  32. func()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement