Python Learning-Decorators

Source: Internet
Author: User

Reference Blog Address: http://www.cnblogs.com/wupeiqi/articles/4980620.html

1. Write code to follow the open closure principle , in short, the provisions have been implemented function code is not allowed to be modified, but can be extended, namely:

closed: Implemented function code block

Open: for extended development


2. the adorner completely replaces the F1 function, and then adds the F1 function body to its function body, as shown in 1:

PS: Note the order in which the code is executed

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/85/2F/wKiom1ecMavTEsi8AABbxohgsdg732.png-wh_500x0-wm_3 -wmp_4-s_370473146.png "title=" 1.png "alt=" Wkiom1ecmavtesi8aabbxohgsdg732.png-wh_50 "/>

Figure 1

Decorator Understanding Customization Figure 2: Overwrite the original F1 function with the function defined by the adorner, so that additional functions can be executed before and after executing the F1 function. Has no relation to the original function.

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/85/2F/wKioL1ecMhvxCZTHAAAXUidzA-w992.png-wh_500x0-wm_3 -wmp_4-s_3003091743.png "title=" 2.png "alt=" Wkiol1ecmhvxczthaaaxuidza-w992.png-wh_50 "/>

Figure 2


3. using the Universal parameter *args,**kwargs to solve the parameter number of the non-uniform situation.


4. Double- decked decorator

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/85/2F/wKiom1ecMkSC8V24AACI5bdy8XQ931.png-wh_500x0-wm_3 -wmp_4-s_1169749436.png "title=" 3.png "alt=" Wkiom1ecmksc8v24aaci5bdy8xq931.png-wh_50 "/>

5. Multilayer Decorative Device


6. More Awesome decorators-can add parameters


Cond


Python Learning-Decorators

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.