Python 3 Study Notes (6) ---- decorator, python Study Notes

Source: Internet
Author: User

Python 3 Study Notes (6) ---- decorator, python Study Notes

I. Definition

The decorator is essentially a function. To decorate other functions, it adds additional functions to other functions.

Ii. decorator principles

1. the source code of the decorated function cannot be modified.

2. The call method of the decorated function cannot be modified.

3. necessary knowledge of the decoration Device

1. The function is a variable.

1 # def foo (): 2 # print ("in the foo") 3 # bar () # bar undefined 4 # foo () 5 6 7 # def bar (): 8 # print ("int the bar") 9 # def foo (): 10 # print ("in the foo") 11 # bar () 12 # foo () 13 14 15 # def foo (): 16 # print ("in the foo") 17 # bar () 18 # def bar (): 19 # print ("int the bar") 20 # foo ()
1 def foo (): 2 print ("in the foo") 3 bar () 4 foo () 5 def bar (): #6 print ("int the bar") is not executed after the call ")

2. High-order functions (two expressions of high-order functions)

2.1 pass a function name as a real parameter to another function (add a function to the function without modifying the source code of the decorated function)

 1 import time 2  3  def bar(): 4      time.sleep(3) 5      print("in the bar") 6  7  def test1(func): 8      start_time = time.time() 9      func()10      stop_time = time.time()11      print("the func run time is %s" %(stop_time-start_time))12  test1(bar)

2.2 The return value contains the function name.

1 import time 2 def bar (): 3 time. sleep (3) 4 print ("in the bar") 5 def test2 (func): 6 print (func) 7 return func # return to memory address 8 9 # print (test2 (bar) 10 # t = test2 (bar) 11 # t () # run bar12 bar = test2 (bar) 13 bar () # The returned memory address plus () can be executed.

3. nested Functions

1 x = 0 2 def a (): 3 x = 1 4 def B (): 5 x = 2 6 def c (): 7 x = 3 8 print (x) 9 c () 10 B () # If B () is not executed here, the function is equivalent to nothing, because no print11 ()

4. decorator = high-order functions + nested Functions

1 import time 2 3 def timer (func): # timer (test) func = test 4 def deco (): 5 start_time = time. time () 6 func () # Run the decored Function test 7 stop_time = time. time () 8 print ("func run time is % s" % (stop_time-start_time )) 9 return deco # return deco memory address 10 11 12 @ timer # equivalent to timer = timer (test). The decorator is always placed in the first 13 def test () of the decorated function (): 14 time. sleep (3) 15 print ("this is test") 16 17 test ()

5. If the decorator function contains Parameters

1 import time 2 3 def timer (func): 4 def deco (* args, ** kwargs): # * args, ** kwargs indicates that the parameter is unknown. 5 start_time = time. time () 6 func (* args, ** kwargs) 7 stop_time = time. time () 8 print ("in the run time is % s" % (stop_time-start_time) 9 return deco10 11 @ timer # test2 = timer (test2) = deco test2 (name) = deco (name) 12 def test2 (age, name, job): 13 time. sleep (1) 14 print ("test2:", age, name, job) 15 16 test2 (23, "Irlo", "seller ")

Vi. decorator use example

Assume that you have added login verification for the webpage.

1 user, pasd = "Irlo", "12345" 2 def auth (auth_type): 3 print ("auth func is", auth_type) 4 def type (func ): 5 def wrapper (): 6 if auth_type = "local": 7 username = input ("username :"). strip () # strip removes the character specified at the beginning and end of the string (space by default) 8 password = input ("password :"). strip () 9 if user = username and pasd = password: 10 print ("\ 033 [1; 32; 1 mauthority of account \ 033 [0 m ") # highlighted format (\ 033 [display mode; foreground color; background color m) 11 res = func () # assign the result to a variable before calling home. Result 12 print ("after authentication") 13 return res # If no result is returned, the execution is complete, the Null Value 14 else: 15 print ("invalid username or password") 16 elif auth_type = "ldap": 17 print ("I just know local, fuck off ") 18 return wrapper19 return type20 21 def index (): 22 print ("welcome to index page") 23 24 @ auth (auth_type = "local") 25 def home (): 26 print ("welcome to home page") 27 return "from home" 28 29 @ auth (auth_type = "ldap") 30 def bbs (): 31 print ("welcome to bbs page") 32 33 index () 34 print (home () # calling home is equivalent to calling wrapper35 home () 36 bbs ()

 

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.