Python-based decorator

Source: Internet
Author: User

Decorative Device

1. General functions

# simple functions and calls def A1 ():     Print ("I am zhangsan") def A2 ():     Print ("I am Lisi") A1 () A2 ()

2. Adding functions before and after functions

defInner (func):Print("Add 1") func ()Print("Add 2")    returnfuncdefA1 ():Print("I am Zhangsan")defA2 ():Print("I am Zhangsan") A1=Inner (A1) A1 () A2=Inner (A2) A2 ()

3. Using adorners

defMyWork (func):definner ():Print("Add 1") func ()Print("Add 2")    returninner@mywork#@mywork is equal to A1=mywork (A1)defA1 ():Print("I am Zhangsan") A1 ()#At execution time, @mywork will take the following function as the parameter of the MyWork function mywork (A1), then execute in function inner, inner within func () =a1 ()

4. Functions for decorating with parameters

defMyWork (func):defInner (ARG):Print("Add 1") func (ARG)Print("Add 2")    returninner@myworkdefA1 (ARG):Print 'I am Zhangsan', ARGA1 ("parameter 1")

5. Functions for decorating dynamic parameters

#merging without parameters, having parameters, multiple arguments can decorate functions with n parametersdefMyWork (func):defInner (*args,**Kwargs):Print("Add 1") func (*args,**Kwargs)Print("Add 2")    returninner@myworkdefA1 ():Print 'I am Zhangsan'@myworkdefA2 (ARG):Print 'I am Zhangsan', Arg@myworkdefA3 (ARG1,ARG2):Print 'I am Zhangsan', ARG1,ARG2A1 () A2 ("parameter 1") A3 ("parameter 1","Parameter 2")

6. Decorate a function that contains a return value

#decorate a function that contains a return valuedefMyWork (func):defInner (*args,**Kwargs):Print("Add 1") AA=func (*args,**Kwargs)Print("Add 2")        returnAAreturninner@myworkdefA3 (ARG1,ARG2):Print 'I am Zhangsan', Arg1,arg2 Li=[1,2,3,4,5,6]    returnLi#returns a listList=A3 ("parameter 1","Parameter 2")#the list equals the return value of innerPrint(list)#The Li list is the return value of the A3, so assigning a func () to the AA in the inner function will give the list a return value of inner.

7. The decorator implements the simple principle of login verification

deflogin (): Name=raw_input ("Enter user name:")    ifName = ="Zhangsan":        returnTrueElse:        returnFalsedefMyWork (func):defInner (*args,**Kwargs): Lo_login=Login ()if  notLo_login:#if Login () returns false            return "user name is wrong!"AA=func (*args,**Kwargs)returnAAreturninner@myworkdefA3 (ARG1,ARG2):Print 'I am Zhangsan', Arg1,arg2 Li=[1,2,3,4,5,6]    returnlilist=A3 ("parameter 1","Parameter 2")#the list equals the return value of innerPrint(list)
View Code

8. Multiple adorners decorate a function

defNewwork1 (func):definner ():Print("newwork1 ago") func ()Print("after Newwork1")    returnInnerdefNewwork2 (func):definner ():Print("newwork2 ago") func ()Print("after Newwork2")    returnInner@newwork2@newwork1defF1 ():Print 'I am Zhangsan'F1 ()" "output: Newwork1 before newwork2 before I am Zhangsan newwork2 after Newwork1" "
View Code

9. Adorner Plus parameters

#3-Layer DecoratordefFilter (A1,A2):defOuter (func):defWrapper (Request,kargs):Print(A1) Result=func (Request,kargs)Print(A2)returnresultreturnwrapperreturnOuteraa=11BB=22@Filter (AA,BB)defIndex (Request,kargs):PrintRequest,kargsindex ("Zhangsan","Lisi")#The @Filter (AA,BB) executes the filter (AA,BB) function first, gets the outer after the return value is stitched into @outer, and then becomes a normal adorner.#A1,a2,request,kargs 4 parameters can be used within the wrapper function

Python-based 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.