Python Forum
problem In using functools in a code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem In using functools in a code
#1
hi
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
Reply


Messages In This Thread
problem In using functools in a code - by akbarza - Oct-06-2023, 10:27 AM
RE: problem In using functools in a code - by buran - Oct-07-2023, 11:44 AM
RE: problem In using functools in a code - by buran - Oct-09-2023, 01:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pip3 install functools failed kintarowonders 1 15,986 Mar-07-2019, 07:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020