python learning progress 13 (return function)

Function as a return value

In addition to higher order function accepts as a function of parameters, also as a function of the result value is returned.

We variable parameters to achieve a summation. Typically, the summation function is defined as:

def calc_sum(*args):
    ax = 0 for n in args: ax = ax + n return ax 

However, if the sum is not required immediately, but later in your code, as required, to calculate how to do again? The sum may not return results, but return function summation:

def lazy_sum(*args):
    def sum(): ax = 0 for n in args: ax = ax + n return ax return sum 

When we call lazy_sum(), the return is not the sum result, but the summation function:

>>> f = lazy_sum(1, 3, 5, 7, 9) >>> f <function lazy_sum.<locals>.sum at 0x101c6ed90> 

Call the function fonly when the real summation of the results:

>>> f()
25

In this example, we in the function lazy_sum, he also defines a function sum, and internal functions sumcan reference external function lazy_sumarguments and local variables, when the lazy_sumfunction returns sum, the parameters and variables are stored in the function returns, this is called " closure (closure) "program structure has great power.

Please note that when we call lazy_sum(), the call will return every time a new function, even if passed the same parameters:

>>> f1 = lazy_sum(1, 3, 5, 7, 9) >>> f2 = lazy_sum(1, 3, 5, 7, 9) >>> f1==f2 False 

f1()And f2()the result of the call independently of each other.

Closure

Noting the function returns a reference within the definition of local variables args, so that, when a function returns a function, its local variables inside a function reference is also new, therefore, together with a simple closure, it is not easy to achieve.

Another issue to note is that the function returns is not executed immediately, but until the call f()was executed. Let's look at an example:

def count():
    fs = []
    for i in range(1, 4): def f(): return i*i fs.append(f) return fs f1, f2, f3 = count() 

In the above example, each cycle creates a new function, then create the three functions are returned.

You might think that call f1(), f2()and f3()the result should be 1, 4, 9, but the actual result is:

>>> f1()
9
>>> f2()
9
>>> f3() 9 

All are 9! The reason is that the function returns a reference to a variable i, but it is not executed immediately. By the time three functions return, the variable to which they refer ihas become 3, so the final result is 9.

 Keep in mind that the return closure: function that returns do not refer to any loop variables or variable subsequent changes occur.

If you must reference the loop variable how to do? Another method is to create a function to bind the current value of the loop variable parameter of the function, regardless of how to change the loop variable follow-up, is bound to the value of the function parameters unchanged:

def count():
    def f(j): def g(): return j*j return g fs = [] for i in range(1, 4): fs.append(f(i)) # f(i)立刻被执行,因此i的当前值被传入f() return fs 

Look at the results:

.

 

 

Original URL: https: //www.liaoxuefeng.com/wiki/1016959663602400/1017434209254976#0

 

Guess you like

Origin www.cnblogs.com/2205254761qq/p/12315359.html