Python Growth Path _ decorator

Source: Internet
Author: User

First, into the adorner

1, first of all we have such a piece of code, this code if it is a function of N business units
1 def F1 (AAA): 2 print (' I am F1 business ') 3 if AAA = = ' F1 ': 4 return ' OK ' 5 6 def f2 (AAA): 7 print (' I am F2 business ') 8 if AAA = = ' F2 ': 9 return ' OK '

Here is when we call the above function, passed the value to AAA, when the value of AAA equals F1 or F2 returns OK

2, the company has n business units, 1 basic platform departments, the basic platform is responsible for providing the underlying functions, such as: Database operations, Redis calls, monitoring API and other functions. When the business unit uses the underlying functionality, it simply invokes the functionality provided by the underlying platform. Then we only need to call the functions of the business unit.

F1 (value)

F2 (value)


3, the company's operation is an unstable, but one day, your boss said, I found a problem is that the business unit calls the basic platform of the function of the time did not verify this is not good so the boss put the work to the request to increase the authentication function and the business unit in the way the function can not be changed


Low A, this a, that's what he did.

What about him? Coordinate with the people who do the basic functions, ask to add the verification module to their own code, so that the entire base platform will not need to change, the result, low a day was dismissed ...


Low b, this b is a look at a has been expelled from the above method can not change the one

1 def F1 (AAA): 2 #验证代码 3 #验证代码 4 print (' I am F1 business ') 5 if AAA = = ' F1 ': 6 return ' OK ' 7 8 def F 2 (AAA): 9 #验证代码 #验证代码 print (' I am F2 business ') if AAA = = ' F2 ': Return ' OK '

What about this guy? A verification code was added before each function, and then two days later, Low B was expelled, and finally the boss gave the job to low C.

Low C sums up two low lessons he did that.

1 #验证函数 2 def verify (): 3 # verify 1 4 # verify 2 5 # Verify 3 6 pass 7 8 def F1 (AAA): 9 Verify (): P Rint (' I am F1 business ') one-if AAA = = ' F1 ': return ' OK ', def f2 (AAA): Verify (): Print (' I am F2 business ') 1 7 if AAA = = ' F2 ': Return ' OK '
What about him? The verification function is written as a function then, each business module calls the eldest brother saw the LOWC realization way, the corner of the mouth smiled a smile, and chatted with LOWC a day
Writing code to follow the development of the closure principle, although in this principle is used in object-oriented development, but also for functional programming, in short, it stipulates that the implemented function code is not allowed to be modified, but can be extended, That is: Closed: implemented function code block open: For extended development if the open closure principle is applied to the above requirements, then it is not allowed to modify the code inside the function f1, F2, the boss gives the low C an implementation scheme:

Adorner (single-layer adorner)

 1 def out (Main):   2     def wra ():  3          #  Verification 1  4          #  Verification 2  5         #  Verification 3   6         return xxx  7      return wra  8    9  @out  10 def f1 (AAA ):  11     print (' I am F1 business ')  12     if aaa  ==  ' F1 ': 13         return  ' OK '  14   15  @out  16 def f2 (AAA):  17     print (' I am F2 business ')  18      if aaa ==  ' F2 ': 19          return  ' OK 

Second, the story (the story is to steal the Silver Horn King) to this end we start to look at the above code
(1) First we need to know that when the function is not parenthesized our function is not executed, it will return the memory address of this function Def aaa (): Print ("OK") print (AAA) <function AAA at 0x0000000000b6ac80 >

(2) then let's explain @out.

@out equals F1 = Out (F1)

What do you mean? @out is a shorthand for Python syntax, and his usefulness is to do it for adorners, and we look at the following example, replacing @out with f1 = Out (F1)

1 Def out (main): 2 def WRA (AAA): 3 Print (' I came in ') 4 CCC = Main (AAA) 5 return CCC 6 RET Urn WRA 7 8 def F1 (AAA): 9 Print (' I am F1 business ') if AAA = = ' F1 ': one return ' OK ', F1 = Out (F1) 14 1 5 S1 = ' F1 ' ff1 = f1 (S1) print (FF1)
(3) Let us explain the next out function, for out, he first receives a value, from the above can be seen that the value he received is F1,F2 memory address, and then received after the value returned to the function Wra, note that F1,F2 is not parentheses so did not execute, Return to WRA also without parentheses so do not execute, then here this function will pause our union call to see the following diagram:
Be sure to separate WRA not equal to F1 but main equals f1!! The following F1 equals WRA means that executing the F1 function equals executing the WRA function.
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7A/CC/wKioL1a4rL7xhSvIAADfeaM8is8457.png "title=" 1.png " alt= "Wkiol1a4rl7xhsviaadfeam8is8457.png"/>
Step Two:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/CC/wKiom1a4rHSxkm2GAAFUkVcJ2y8073.png "title=" 2.png " alt= "Wkiom1a4rhsxkm2gaafukvcj2y8073.png"/>

And finally, we're going through the Python syntax pool, f1 = Out (F1) into @out, which is the code we saw before.
(4) The adorner loaded with parameters of the function, some students may test said, I put the function F1 parameters into a number of, the decorator on the error, that is how it happened, the reason is very simple, because WRA first he can only receive a parameter, and CCC = Main (AAA) also only received a parameter def Out (main): Def WRA (AAA): print (' I came in ') CCC = Main (AAA) return CCC return WRA


  Now that we know the reason, we'll change it. Digital Baby Super Evolution ... :  1 def out (Main):    #这里就不多解释了跟上面一样   2      Def wra (*AAA,**AA):   #这里呢变成了啥是不是可以接收各种的参数了   3          print (' I came in ')   4         ccc =  main (*AAA,**AA)   #这里呢也可以给f1各种参数   5          Return ccc  6     return wra  7   8   @out   9 def f1 (*AF1):  10     print (' I am F1 business ')  11      print (Af1[0])   #给大家测试用的  12     print (af1[1])   13     if af1[0] ==  ' F1 ': 14          return  ' OK '  15  16 s1 =  ' F1 '  17 ff1&NBSP;=&NBSP;F1 (S1, ' I am parameter 2 ')   #传入了两个值s1和我是参数2 '  18 print (ff1)    #运行一下看看吧呵呵 
Third, the ultimate evolution of the adorner (multilayer adorner) logic comparison around

One day, the abnormal boss and found a low C said your decorator practice how, Low C said: After the eldest, I have practiced almost, this time the eldest son of a smile, good, so I have a need you to change me, I now want to be validated after the addition of a welcome function, This function, our line of business functions want to add to add the first or add not to add, remember the closure principle oh 0.0 ...

The next day low C found the eldest brother said, you still come to my tutor at night to teach me, really do not know ah 0.0,,, and then the eldest brother went to the low C's home after a wind and cloud (this omitted 10,000 words) 0.0 eldest brother provided additional reference code:

 1 def ds ():   2     print (' OK I'm welcome info 1 ')   3    4 def ss ():   5     print (' OK I'm welcome info 2 ')   6    7   8 def fill (*ill):   9     def  out (Main):  10         def wra (*waa,**wad):  11             ill[0] ()  12   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ILL[1] ()  13              ccc = main (waa[0])  14              return ccc 15          return wra 16     return out 17   18  19  @fill (DS,SS)  20 def f1 (AAA):  21     print (' I am F1 business ')  22      if aaa ==  ' F1 ': 23          return  ' OK '  24  25  26 c1 = f1 (' F1 ')  27  Print (c1)   haha don't understand it (except for the Big Gods) come on, we'll see for a split-up.
The first step: or explain the adorner fill please look at the picture, there is a word to remember this decorator I will interpret it as a call, that is, the need to pass the function into the adorner to do the value, so call the value to execute the function
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7A/CC/wKiom1a4rVbBIkBFAAFmM8qBdJE917.png "title=" 3.png " alt= "Wkiom1a4rvbbikbfaafmm8qbdje917.png"/>
Step Two:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7A/CC/wKiom1a4rWXSPLFSAAEg6YrO68M704.png "title=" 4.png " alt= "Wkiom1a4rwxsplfsaaeg6yro68m704.png"/>

Good to here everyone should understand, do not understand the message, say wrong please advise thank O (∩_∩) o~~, then some small partners to ask, this Nima wrong ah, for Mao I put @fill (DS,SS) into @fill (ss) on the error?

We analyze the problem, the main reason is the Def WRA function under the execution of Ill[1] error, because here *ill only one parameter so avoid it, see my ultimate big recruit:

 1 def ds ():  2     print (' OK I'm welcome info 1 ')   3 def  SS ():   4     print (' OK I'm welcome info 2 ')   5   6 def  fill (*ill):   7     def out (Main):  8          def wra (*waa,**wad): #这里加个判断不就完了么  o (∩_∩) O haha ~  9              if len (ill)  !=  ' 0 ':  10                  For i in range (0,len (ill)): 11                      ill[i] ()  12              ccc = main (waa[0])  13              return ccc 14          return wra 15     return out 16  17  @fill (ss , DS)  18 def f1 (AAA):  19     print (' I am F1 business ')  20      if aaa ==  ' F1 ': 21          return  ' OK '  22  23  @fill ()     #你看这里没参数吧  24 def f2 (AAA):  25     print (' I am F2 business ')  26     if aaa ==   ' F2 ': 27         return  ' OK '  28   29&NBSP;C1&NBSP;=&NBSP;F1 (' F1 ')  30 print (C1)  31 c2 = f2 (' F2 ')  32 print (C2)


#运行下试试吧, welcome, and so on! The last boss and low C became ... 10,000 characters omitted here
I have to vomit a sentence, please 51cto with windowslive writer good compatibility with it
Blog Park address: http://www.cnblogs.com/bj-xy/p/5185306.html

This article is from the "Liumingyuan blog" blog, make sure to keep this source http://liumingyuan.blog.51cto.com/9065923/1741415

Python Growth Path _ decorator

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.