Oct-06-2023, 10:27 AM
hi
in code:
when a module is imported, for using its function, only the function is called. for example, I write:
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
in code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# 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__}") |
1 2 |
import math math.exp( 1 ) |
thanks