Python Note day5

Source: Internet
Author: User
Tags closure

Http://www.cnblogs.com/wupeiqi/articles/4938499.html

Python Interpreter execution order: top to bottom

deffoo ():--reads the Foo function into memory, but does not execute the function body content
Print' abc ' --Skip the line .
Foo --representsFoofunction,Foomemory address pointing to function
Foo ()--represents the executionFoofunction that takes the function body content backPrint' abc '
Foo =Lambdaa:a +1-->fooa memory address that points to another function that implements the function ofa+1

foo ()-- Execution a+1 The function body of this function

Function Call Function:

def F1 (ARG):
Arg ()
def func ():
print "func"
F1 (func)

the Python interpreter executes from the top down

First step: Execute def F1 (ARG): writes a function to memory, but does not execute the contents of the function body Arg ()

Step Two: Execute def func (): writes a function to memory, but does not execute the contents of the function body print ' func '

Step three: Execute F1 (func) calling Functions F1

Fourth Step: Execute def F1 (ARG): , then arg = func , Arg memory address pointing to function

Fifth Step: Execute Arg () , arg () = func () , i.e. execution func () function

Sixth step: Execute def func ():

Seventh Step: Execute print ' A '

Business Scenario:

basic platform operations to write a variety of basic functions, providing a variety of API to the production line, to query the database, the use of various machines and other functions

before calling API does not do certification, now want to add the auth () function to do certification, the basic practice:

Auth ():
Xxxxxx
F1 ():
Auth ()-- add auth () function to the function body to do the authentication
"F1"
F2 ():
Auth ()-- add auth () function to the function body to do the authentication
"F2"

However, due to the open closure principle:

the function body cannot be modified, only the configuration file can be modified or Main () function to modify the order in which the functions are executed, or to add new functions to change the functionality of the implementation

For example, the original defined function execution order is 1->2->3->4 , now modified to 1->2->2.5->4->3 , adding a 2.5 function, and swapped the 3 and the 4 the Order

So it needs to be written like this:

650) this.width=650; "Width=" to "height=" "src="/e/u261/themes/default/images/spacer.gif "style=" background: URL ("/e/u261/themes/default/images/word.gif") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>defAuth (func):
defInner():
Print' Verify function '
Func ()
returnInner
defF1 ():--Original function
Print ' F1 '
retval = Auth (F1)
retval ()

First step: Execute def Auth (func):, load the auth () function into memory, but do not execute the function body contents

Step Two: Execute Def F1 ():, load the F1 () function into memory, but do not execute the function body contents

Step Three: Execute retval = Auth (F1), call the Auth () function, and pass the parameter F1

Fourth step: Execute def Auth (func):, then func = = F1

Fifth step: Execute Def inner ():, Load the auth () function into memory, but do not execute the function body of the inner () function

Sixth step: Execute return inner, then retval = = Inner,retval is equal to the entire inner () function body

Seventh Step: Execute retval (), retval () ==inner (), which is the execution of the inner () function body, the inner () function body has func (), and Func () ==f1, executes the original function F1

if the retval = Auth (F1) , rewritten as f1= Auth (F1) (the first one F1 is the new F1 , a second F1 it's old. F1 function) Then the line-of-business would be free to change the code.

python provides a simple notation, which is the adorner:

  def   Span style= "FONT-SIZE:12PX;" >auth (func): 
  def inner ():
    print " authentication function '
   
func ()
'  Print log '
Inner
650) this.width=650; "Width=" height= "src="/e/u261/themes/default/images/spacer.gif "style=" background: URL ("/e/u261/themes/default/images/word.gif") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>650) this.width=650; "Width=" height= "174" src= "/e/u261/themes/default/images/spacer.gif" style= "background: URL ("/e/u261/themes/default/images/word.gif") no-repeat center;border:1px solid #ddd; "alt=" Spacer.gif "/>defAuth_arg1 (func):
defInner (arg):
Print' authentication function '
func (arg )
      print     "    print log '    
  return inner
@ auth  = = def f1 ():
  print ' F1 '
@ Auth_arg1 = = "Executes f1 = Auth (F1), which is the return value of the inner () function copied to F1
F2 (arg):
' F2,arg '
The essence of an adorner is to execute a auth function before executing the F1 function without modifying the F1 function body.
With the adorner, the base platform to 500 functions to increase the authentication function, without breaking the open closure principle, only need to write a auth function and 500 @auth,auth function inner sub-function to implement the authentication function, the line of business does not need to make any changes, The function that is implemented is that the F1 function is authenticated before it is executed, and the F1 function prints the log
When the function F2 has a parameter, it is necessary to write an adorner auth_arg1 with a parameter, and write @auth_arg1 before F2.

On the 5th day, we should see 06.


Python Note day5

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.