Long path to Go Python 4

Source: Internet
Author: User

Adorner decorator

(the "device" stands for a function)

definition : The essence is a function

function : used to decorate other functions in other words, add additional functions for other functions

(eg. If the program is online, if you want to add functionality, but can not change the source code)

Principle:

1. Cannot modify the source code of the decorated function

2. The caller of the decorated function cannot be modified

composition : higher order functions + nested functions--decorators

Q: What is a high-order \ Nested function???

Higher order functions:

1. Pass a function as an argument to another function (add a function to it without modifying the source code of the decorated function)

Import TimedefBar (): Time.sleep (3)    Print("In the bar")defTest1 (func): Start_time=time.time () func ()#Run bar ()Stop_time =time.time ()Print("The func run time is%s"% (Stop_time-start_time))#statistics on the bar's run timetest1 (BAR)#func = Bar So the memory address of func () can be passedTest1 (Bar ())#The return value of Bar () is passed in the return value#but! The calling style changed, not the adorner .
View Code

2. The return value contains the function name (does not modify the function's calling method)

 import   time  def   bar (): Time.sleep ( 3 print  ("  in the bar   " )  def  Span style= "COLOR: #000000" > Test2 (func):  print  (func) #   print func memory address, i.e. bar's memory address   Return  func #   return bar memory address  bar  = test2 (bar) bar ()  #   run bar  
View Code

Nested functions:

Use Def to apply for another function in the function body of a function

 #   nested functions   def   foo ():  print  ("  in the foo   " )  def  Bar (): #
     print
       ( " in the bar   " ) bar () foo ()  #   This is not a nesting of functions, this is called a function call  def   Test1 (): Test2 () test1 ()  
View Code

EXAMPLE 1

Optimization (@timmer)

defTimer (func):#the timer (test1) passes Test1 's memory address to Func,func = Test1    defdeco (): Start_time=time.time () func ()#Run Test1Stop_time =time.time ()Print("The func run time is%s"% (start_time-stop_time)) returnDeco#returns the memory address of this function@timer#this is equivalent to Test1 = Timer (test1)deftest1 (): Time.sleep (3)    Print("In the test1") @timerdeftest2 (): Time.sleep (3)    Print("In the test2") test1 () test2 ()#This is actually called the Deco function, because the return value of the timer (TEST1) is the memory address of Deco
View Code

Optimization (incoming multiple parameters upon invocation)

defTimer (func):#the timer (test1) passes Test1 's memory address to Func,func = Test1    defDeco (*args,**Kwargs): Start_time=time.time () func (*args,**kwargs)#Run Test1Stop_time =time.time ()Print("The func run time is%s"% (start_time-stop_time)) returnDeco#returns the memory address of this function@timer#this is equivalent to Test1 = Timer (test1) = Decodeftest1 (): Time.sleep (1)    Print("In the test1") @timer#test2 = Timer (test2) = Deco#test2 () = Deco () So in deco add argsdeftest2 (name,age):Print("test2:", Name,age,) test1 () test2 ("Alex", 22)
View Code

EXAMPLE 2!!

Importtimeuser,passwd="Alex","abc123"defAuth (auth_type):Print("auth func:", Auth_type)defOuter_wrapper (func):defWrapper (*args,**Kwargs):Print("wrapper func args:", *args,**Kwargs)ifAuth_type = ="Local": Username= Input ("Username:"). Strip () password= Input ("Password:"). Strip ()ifuser = = Username andpasswd = =Password:Print("\033[32;1muser has passed authenfication\033[0m") Res= Func (*args,**kwargs)#return from home                   Print("------after Authenficaiton")                   returnResElse: Exit ("\033[31;1minvalid username or password\033[0m")             elifAuth_type = ="LADP":                   Print("yarn LDAP, will not------")        returnwrapperreturnOuter_wrapperdefIndex ():#Home Page    Print("Welcome to Index page") @auth (Auth_type="Local")#Local Authentication & home = wrapper ()defHome ():#home page After the user logs in    Print("Welcome to Home Page")    return "From home"@auth (Auth_type="LDAP")#Remote AuthenticationdefBBS ():#Forum    Print("Welcome to BBS page") index ()Print(Home ())#return None If there is no return func ()BBS ()
View Code

Ps:

Functions and variables

#no problem.defBar ():Print("In the bar")deffoo ():Print("In the foo") bar () foo ()#bar definition to the following is also possible to rundeffoo ():Print("In the foo") bar ()defBar ():Print("In the bar") foo ()#will error, because the function and the variable, is defined first, in the call. So it 's an error.deffoo ():Print("In the foo") bar () foo ()defBar ():Print("In the bar")
View Code

Generator generator

principle: The corresponding data is generated only when the call is made (if a big data is only broken by the first 5 times, then the data behind is not ready)

function: Save memory

Features: The generator only remembers the current position (no matter the front or back), can not return, nor jump.

There is only one next method: __next__ ()

Ps:

List-Generated

>>>print for in range (2, 4, 6, 8, ten, A, +, +) for inch # The pass function can also

Long path to Go Python 4

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.