Decorator and python decorator

Source: Internet
Author: User
Tags ldap python decorator

Decorator and python decorator

1. What is a decoration device?

The decorator is essentially a function. Its function is to decorate other functions and add additional functions to other functions.

2. Application scenarios

If our program has been launched, we want to add new features for it. We originally wanted to modify the source code, but this poses a certain risk,

In this case, you can write a modifier (new function) to add some modifications to the new function.

3. decorator principles

3.1 you cannot modify the source code of the decorated function.

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

3.3 The ornament is transparent to the decorated functions.

4. Implement the knowledge reserve of the decorator:

4.1 Functions are "variables"

4.2 high-order functions (what is high-order functions)

A.Function NamePass as a real parameter to another function (add new functions without modifying the source code to be decorated)

B. the return value contains the function name (the function call method is not modified)

4.3 nested functions (what is a nested function)

In a function, use def to declare another function.

# Nested function def foo (): print ("in the foo") def bar (): print ("in the bar") bar () # bar cannot be called externally, because it is equivalent to a local variable foo () # The following is a function call, not nested def test1 (): test2 () test1 ()

5. decorator example

5.1 Example 1

Import timedef timmer (func): # input timmer (test1) func = test1 def deco (): start_time = time. time () time. sleep (1) func () # run test1 () stop_time = time. time () print ("the run time is % s" % (stop_time-start_time) return deco @ timmer # test1 = timmer (test1) def test1 (): print ("in the test1") # You can use the Python syntax sugar @ to add the function money to be decorated # test1 = timmer (test1) # timmer (test1) the result is the function body --> return deco's memory address print (test1) # print test1, that is, deco's memory address test1 ()
View Code

First, write a high-level function to implement"Add new features without modifying the source code to be decorated "----Pass the function test1 as a real parameter to the timmer function.

ToDoes not change its call MethodThe nested function is used to get the returned function name warrper.

5.2 Example 2 --- Parameter Function annotator

Import timedef timmer (func): # func = test2 def deco (args): # Add the text2 (name) parameter args start_time = time. time () time. sleep (3) func (args) # execute args stop_time = time. time () print ("the func run time is % s" % (stop_time-start_time) return deco # return deco memory address @ timmerdef test2 (name ): print ("in the test2:", name) test2 ('frank') # test2 () = deco (). To add a parameter to test2 (), add a parameter to deco.
View Code

5.3 Example 3 --- decorator for any function

Import timedef timmer (func): def deco (* args, ** kwargs): # If a parameter is added, if start_time = time is not added. time () time. sleep (3) func (* args, ** kwargs) # Run the parameter. If so, stop_time = time. time () print ("the func run time is % s" % (stop_time-start_time) return deco # return deco memory address @ timmerdef test2 (name ): print ("in the test2:", name) @ timmerdef test1 (): print ("in the test1") test2 ('frank') # test2 () = deco (), to add a parameter to test2 (), add test1 () to deco ()
View Code

5.4 Example 4 --- release version --- the decoration device also has Parameters

#! C: \ Program Files \ Python35/bin #-*-conding: UTF-8-*-# author: Frankdef auth (auth_type): # absorb the first layer parameters, decorator parameter # print ("auth func", auth_type) def out_warppage (func): # absorb the second-layer parameter and pass the function into func = home def warppage (* args, ** kwargs): if auth_type = "local": user, pwd = 'frank', '000000' username = input ("Username is :") password = input ("Password is:") if username = user and password = pwd: print ("\ 033 [32; 1 myou had login succesfull \ 033 [0 m ") res = func (* args, ** kwargs) # run home (), index (), bbs () return res else: exit ("\ 033 [31; 1 m Invaild user/password \ 033 [0 m") else: print ("Ldap won't") return warppage return out_warppagedef index (): print ("welcom to index page") @ auth (auth_type = "local") # When the decorator also has parameters, we need to nest the layer, # pass the parameters of the decorator to the top-level function as the real parameter def home (): print ("welcom to home page") return "from home" @ auth (auth_type = "ldap ") def bbs (): print ("weblcom to bbs") index () bbs () home () # home = warapper
View Code

When syntax sugar also has parameters, we need to nest a Layer

A. The parameters of the syntactic sugar are passed into the top-level function, and the method in which the function is loaded ("local" or "ldap ")

B. The modified function is passed into the second layer as a real parameter.

C. After completing steps a. B, start to execute the function. At this time, a. B is loaded, so that you can freely make judgments.

 

Related Article

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.