Python-based decorator and python-based Decoration

Source: Internet
Author: User

Python-based decorator and python-based Decoration

Decorator

1. Common functions

# Simple functions and calls def a1 (): print ("I am zhangsan") def a2 (): print ("I am lisi") a1 () a2 ()

2. Add features before and after the Function

Def inner (func): print ("Add 1") func () print ("add 2") return funcdef a1 (): print ("I am zhangsan ") def a2 (): print ("I am zhangsan") a1 = inner (a1) a1 () a2 = inner (a2) a2 ()

3. Use decorator

Def mywork (func): def inner (): print ("Add 1") func () print ("add 2 ") return inner @ mywork # @ mywork is equivalent to a1 = mywork (a1) def a1 (): print ("I am zhangsan") a1 () # during execution, @ mywork regards the function below as the parameter of mywork function, namely mywork (a1), and then runs it in the inner of the function. func () = a1 () in the inner ()

4. decorative function with Parameters

Def mywork (func): def inner (arg): print ("Add 1") func (arg) print ("add 2") return inner @ myworkdef a1 (arg ): print 'I am zhangsan', arga1 ("parameter 1 ")

5. Function for decorating Dynamic Parameters

# Merge the functions without parameters and with parameters. The function def mywork (func): def inner (* args, ** kwargs) containing N parameters can be decorated with multiple parameters ): print ("Add 1") func (* args, ** kwargs) print ("add 2") return inner @ myworkdef a1 (): print 'I am zhangsan' @ myworkdef a2 (arg): print' I am zhangsan', arg @ myworkdef a3 (arg1, arg2): print 'I am zhangsan', arg1, arg2a1 () a2 ("parameter 1") a3 ("parameter 1", "parameter 2 ")

6. Describe the functions with returned values.

# Describe the function def mywork (func) containing the returned value: def inner (* args, ** kwargs): print ("Add 1") aa = func (* args, ** kwargs) print ("add 2") return aa return inner @ myworkdef a3 (arg1, arg2): print 'I am zhangsan', arg1, arg2 li = [1, 2, 2, 3, 4, 5, 6] return li # returns a list = a3 ("parameter 1", "parameter 2") # list is equal to the inner's return value print (list) # The li list is the return value of a3. Therefore, the func () executed in the inner function is assigned to aa, and the list can be obtained through the return value of inner.

7. Simple login verification principle using the decorator

Def login (): name = raw_input ("input Username:") if name = "zhangsan": return True else: return Falsedef mywork (func): def inner (* args, ** kwargs): lo_login = login () if not lo_login: # if login () returns False return "username error! "Aa = func (* args, ** kwargs) return aa return inner @ myworkdef a3 (arg1, arg2): print 'I am zhangsan', arg1, arg2 li = [1, 2, 3, 4, 5, 6] return lilist = a3 ("parameter 1", "parameter 2") # list is equal to the returned value of inner print (list)View Code

8. Decoration of one function with multiple decorators

Def newwork1 (func): def inner (): print ("before newwork1") func () print ("after newwork1") return innerdef newwork2 (func): def inner (): print ("before newwork2") func () print ("after newwork2") return inner @ newwork2 @ newwork1def f1 (): print 'I am zhangsan' f1 () ''' output result: Before newwork1, newwork2, I am zhangsan newwork2, newwork1, and then '''View Code

9. Add parameters to the decorator

#3 layer decorator def Filter (a1, a2): def outer (func): def wrapper (request, kargs): print (a1) result = func (request, kargs) print (a2) return result return wrapper return outeraa = 11bb = 22 @ Filter (aa, bb) def Index (request, kargs): print request, kargsIndex ("zhangsan ", "lisi") # @ Filter (aa, bb) First executes the Filter (aa, bb) function, obtains the returned value outer, and concatenates it into @ outer, then it becomes a common decorator. # Four parameters a1, a2, request, and kargs can be used in the wrapper function.

 

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.