function advanced function of Python basis as return value/adorner

Source: Internet
Author: User

Because the adorner needs to use the knowledge of the return function, the return function and adorner are explained here.

What is a return function?

We know that in a function, return can return one or more values, but in fact, return can return not only the value, but also the function.

Instance:

defCol (*Arg):defsum (): Res_sum=0 forIinchArg:res_sum=res_sum+IreturnRes_sumreturnSuma=col (1,2,3,4,5)Print(a)
<function Col.<locals>.sum at 0x029102b8>
#A=col (1,2,3,4,5) ==〉#that is a=sum, and arg= (1,2,3,4,5) is also passed into sum.#A () ==sum ()

and because sum () is defined in the col () function, sum () inherits the local variables and arguments of the col () function, which is the closure . (for example, the arg parameter of Col () is inherited by sum ())

Let's look at a question I encountered when I checked the above red sentence:

#or use the code above. Change it a little bit.defCol (*Arg): res_sum =0         #Note: Move this word here.    defsum (): forIinchArg:res_sum=res_sum+IreturnRes_sumreturnSuma=col (1,2,3,4,5)Print(A ()) result error: local variable'Res_sum'referenced before assignment#since intrinsic functions can refer to variables of external functions, why is res_sum not referenced by the # part function? 

Why did you get an error? I was puzzled at the time and finally figured it out, wrong in this sentence:res_sum=res_sum+I This sentence causes the internal function to modify the external function of the local variables, and this is not allowed!!!!

Therefore, this modification is required:

def Col (*Arg):    res_sum=0             def  sum ():        for in  ARG:            a=res_sum+i     # defines a new variable a        in the intrinsic function return a     return suma=col (1,2,3,4,5)print(A ())

What is an adorner?

def login ():     Pass @login       #关键字 @  def open ():     Pass

The example above is an example of an adorner.

The role of adorners:

After we have defined the Open function (which can help us to open a file), after a while, we find that we need to verify the user before the open, how to combine the login function and the open function without modifying the Open function , That is: Run login before running open? The decorator will be used here.

Let's see an example of a real decorator:

def Login (Fun):     def Real_login ():         Print ('pleaseinput your password')         return Fun ()     return Real_login@login def open ():     Print ('helloworld') open ()

@login is equivalent to Open=login (open), which rescue the open function.

When we run open ():

open=Login (Open)
  Parameters and local variables in the #login (open): Fun=open (original)
  # The return value of Open=login (open)! #即:open=real_login
  
Open () =real_login () # because real_login is defined in login, it inherits its arguments and local variables # so look at the whole open ( ) ==login (Open) +real_login () +open ()

function advanced function of Python basis as return value/adorner

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.