[Python] an adorner with parameters and passing parameters

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.