function as return value
Higher-order functions can also return a function as a result value, in addition to the ability to accept functions as parameters.
Let's implement the summation of a mutable parameter. In general, the function of summing is defined like this:
def calc_sum(*args): 0 forin args: ax = ax + n return ax
But what if you don't need to sum it right away, but in the later code, and then calculate it as needed? You can return the SUM function without returning the result of the summation!
def lazy_sum(*args): def sum(): 0 forin args: ax = ax + n return ax return sum
When we call Lazy_sum (), we return the SUM function instead of summing the result:
>>> f = lazy_sum(13579)>>> 0x10452f668>
The result of summing is actually computed when the function f is called:
>>> f()25
In this example, we define the function sum in the function lazy_sum, and the inner function sum can be referenced 外部函数lazy_sum的参数和局部变量 , and when Lazy_sum returns the function sum, the relevant parameters and variables are stored in the returned function, which is called "Closure (Closure)" The program structure has great power.
Note again that when we call Lazy_sum (), each call returns a new function, even if the same parameter is passed in:
>>> f1 = lazy_sum(13579)>>> f2 = lazy_sum(13579)>>> f1==f2False
The invocation results of F1 () and F2 () do not affect each other.
Closed Package
Notice that the returned function references the local variable args within its definition, so when a function returns a function, its internal local variables are referenced by the new function, so it is not easy to implement them with a simple closure.
Another problem to be aware of is that the returned function is not executed immediately, but not until F () is called. Let's look at an example:
def count(): fs = [] forin range(14): def f(): return i*i fs.append(f) return fsf1, f2, f3 = count()
In the example above, each loop creates a new function and then returns the 3 functions that were created.
You might think that calling F1 (), F2 (), and F3 () results should be 1,4,9, but the actual result is:
>>> f1()9>>> f2()9>>> f3()9
All of them are 9! The reason is that the returned function refers to the variable I, but it is not executed immediately. When all 3 functions return, they refer to the variable i has become 3, so the final result is 9.
One thing to keep in mind when returning closures is that the return function does not refer to any loop variables, or to subsequent variables that change.
What if you must refer to a loop variable? The method is to create a function that binds the current value of the loop variable with the parameter of the function, regardless of how the loop variable is subsequently changed, and the value that is bound to the function parameter remains the same:
>>> def Count (): ... FS = [] ... for i in range (1 , 4 ): ... def f (j): ... def g (): ... return j*j ... return g ... Fs.append (f (i)) return fs ... >>> F1, F2, F3 = count () >>> F1 () 1 >>> F2 () 4 >> > F3 () 9
The disadvantage is that the code is long and can be shortened with a lambda function.
Python's learning notes function as a return value, closure explanation