importtimedefexpensive_function()->None:"""This is a function that does something expensive."""time.sleep(2)print("I'm done!")defmain()->None:expensive_function()prnt(expensive_function.__name__)print(expensive_function.__doc__)print(expensive_function.__annotations__)main()
importtimefromtypingimportCallable,Anydefget_time(func:Callable)->Callable:"""Get the time of function execution."""defwrapper(*args,**kwargs)->Any:"""Wrapper function."""start_time:float=time.perf_counter()result:Any=func(*args,**kwargs)end_time:float=time.perf_counter()print(f"Function {func.__name__} took {end_time-start_time:.4f} seconds.")returnresultreturnwrapper@get_timedefexpensive_function()->None:"""This is a function that does something expensive."""time.sleep(2)print("I'm done!")defmain()->None:print(expensive_function.__name__)# wrapper print(expensive_function.__doc__)# Wrapper function.print(expensive_function.__annotations__)# {'return': typing.Any}expensive_function()main()
importtimefromtypingimportCallable,Anyfromfunctoolsimportwrapsdefget_time(func:Callable)->Callable:"""Get the time of function execution."""@wraps(func)defwrapper(*args,**kwargs)->Any:"""Wrapper function."""start_time:float=time.perf_counter()result:Any=func(*args,**kwargs)end_time:float=time.perf_counter()print(f"Function {func.__name__} took {end_time-start_time:.4f} seconds.")returnresultreturnwrapper@get_timedefexpensive_function()->None:"""This is a function that does something expensive."""time.sleep(2)print("I'm done!")defmain()->None:print(expensive_function.__name__)# expensive_function print(expensive_function.__doc__)# This is a function that does something expensive.print(expensive_function.__annotations__)# {'return': None}expensive_function()main()