Scene when such, I have a lot of tasks, I want to give these 任务计时
, warehousing. An adorner with parameters is required to record the 任务
name, and it is necessary to document the time the task was executed before and after the task was executed.
#-*-encoding=utf-8-*- fromFunctoolsImportWraps def task_logging(taskname): def func_wrapper(func): @wraps (func) def return_wrapper(*args, **wkargs): # The function transmits parameters to the adorner by decorating the parameters Print ' before Task ', TaskName# Decorator Pass variable to functionTaskID =1Summer, Funcres = Func (TaskID, *args, **wkargs)Print ' after Task ', TaskID, SummerreturnFuncresreturnReturn_wrapperreturnFunc_wrapper@task_logging ("test") def testd(taskid): Print "Testd runing", TaskIDreturn "task summer success EG", []PrintTESTD ()
Results of execution
task1task1task summer success eg[]
Replace the middle print with the action that needs to be performed, the function of the response, and the 装饰器
parameters 被装饰函数
can be passed well between and.
[Python] an adorner with parameters and passing parameters