Oct-06-2023, 10:27 AM
hi
in code:
thanks
in code:
# from: https://python.coderz.ir/lessons/l13-decorator-generator-and-lambda-with-python-functions.html import functools import time def timer(func): """Print the runtime of the decorated function""" # @functools.wraps(func) # def wrapper_timer(*args, **kwargs): start_time = time.perf_counter() value = func(*args, **kwargs) end_time = time.perf_counter() run_time = end_time - start_time print(f"Finished {func.__name__!r} in {run_time:.4f} secs") return value return wrapper_timer @timer def waste_some_time(num_times): """This function only wastes some times.""" result = 0 for _ in range(num_times): for i in range(10000): result += i**2 if __name__=="__main__" : print(f" result of running command 'waste_some_time(1)' is :") waste_some_time(1) print() print(f" result of running command 'waste_some_time(999)' is :") waste_some_time(999) print(f"result of command 'waste_some_time.__name__' is: \ {waste_some_time.__name__}") print(f"result of command 'waste_some_time.__doc__' is: \ {waste_some_time.__doc__}")when a module is imported, for using its function, only the function is called. for example, I write:
import math math.exp(1)but in line 8 of the above code, there is @ functools.wraps(func). why this @is used for? I read about wraps a little value, but I did not understand it. for what we use this?
thanks