Adorners decorate with parameters of functions and add functions

Source: Internet
Author: User

Modifier with parametric function

1. Func1 with parametric function

def func1 (ARG):     Print arg

2. Decorative Device

def outer (fun):     def Wrapper (ARG):         Print ' Test ' Fun         (ARG)    return Wrapper

Adorner outer time return function wrapper, because to func1 with parameters, if you need to take the wrapper function with parameters, func1 is decorated and redefined as:

Func1 (ARG) =wrapper (ARG) =                         {                             print'test'                             func1 (ARG)                                  }

Using function decorations in decorators

The additional functions to be added are

def before ():     Print ' before '    def After ():     Print '  After '

Principal function:

def Main ():     Print ' Main '

Adorner:

defFilter (Before_func,after_func): function name after #要在主体函数前 and functiondefouter (main_func):defwrapper (): Before_result=Before_func () #如果前函数不为None, the executionif(before_result!=None):returnBefore_result Main_result=Main_func () #执行主体函数if(main_result!=None):returnMain_result After_result=After_func () #如果后函数不为None, the executionif(after_result!=None):returnAfter_resultreturnwrapperreturnOuter

Execute the result, print out:

Before
Main
After

Adorners decorate with parameters of functions and add functions

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.