Feb-09-2021, 10:30 AM
Hello everyone,
considering the code I've written here, please tell me:
1. Is this done correctly? (Although I get the correct output, someone told me for functions that take inputs, I need a wrapper within another wrapper!)
2. How can I do it differently? (two wrappers?! two decorators?!)
considering the code I've written here, please tell me:
1. Is this done correctly? (Although I get the correct output, someone told me for functions that take inputs, I need a wrapper within another wrapper!)

2. How can I do it differently? (two wrappers?! two decorators?!)
import time def elapsed_time(function): def wrapper(*args): t1 = time.time() function(*args) t2 = time.time() print(f'Elapsed time: {(t2 - t1) * 1000:.2f} ms') return wrapper @elapsed_time def sleep(seconds): # a function that DOES take argument(s) time.sleep(seconds) sleep(0.5)
Output:Elapsed time: 514.42 ms
Special thanks to the contributors.