Python Fifth day Decorator

Source: Internet
Author: User

Decorative Device

Adorners are functions, except that the function can have special meanings, adorners are used to decorate functions or classes, and adorners can be used to add actions before and after the function executes.

1 defWrapper (func):2     defresult ():3         Print 'before'4 func ()5         Print ' After'6     returnresult7  8 @wrapper9 deffoo ():Ten     Print 'Foo'
1 ImportFunctools2 3 4 defWrapper (func):5 @functools. Wraps (func)6     defwrapper ():7         Print 'before'8 func ()9         Print ' After'Ten     returnwrapper One  A @wrapper - deffoo (): -     Print 'Foo'
1 #!/usr/bin/env python2 #Coding:utf-83  4 defbefore (Request,kargs):5     Print 'before'6      7 defAfter (Request,kargs):8     Print ' After'9  Ten   One defFilter (before_func,after_func): A     defouter (main_func): -         defWrapper (Request,kargs): -               theBefore_result =Before_func (Request,kargs) -             if(Before_result! =None): -                 returnBefore_result; -               +Main_result =Main_func (Request,kargs) -             if(Main_result! =None): +                 returnMain_result; A               atAfter_result =After_func (Request,kargs) -             if(After_result! =None): -                 returnAfter_result; -               -         returnwrapper -     returnouter in       - @Filter (before, after) to defIndex (Request,kargs): +     Print 'Index' -       the       * if __name__=='__main__': $Index (+)

Python Fifth day Decorator

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.