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.
To implement the summation of a mutable parameter, the function is usually defined like this:
def calc_sum (*args): Ax = 0 for n in args:ax = ax + n return ax
But what if you don't need to sum it now, and then calculate it in the later code as needed? Instead of returning the result of a sum, you can return a function that sums:
def lazy_sum (*args): def sum (): Ax = 0 for n in args:ax = ax + n return ax return su M
When we call Lazy_sum (), the return is not the result of the summation, but the summation function:
>>> f = lazy_sum (1, 3, 5, 7, 9) >>>f<function lazy_sum.<locals>.sum at .....>
The result of summing is actually computed when the function f is called:
>>>f (25)
In the above example, we define the function sum in the function lazy_sum, and the inner function sum can refer to the parameters and local variables of the external function lazy_sum, and when Lazy_sum returns the function sum, the relevant parameters and variables are saved in the returned function, which is called " The program structure of the closure (Closure) has great power.
Note again that when we call Lazy_sum, each call returns a new function, even if the same parameters are passed in:
>>> f1 = lazy_sum (1, 3, 5, 7, 9) >>> F2 = lazy_sum (1, 3, 5, 7, 9) >>> F1 = = F2false
The invocation results of F1 () and F2 () do not affect each other.
Closed Package
From the above example, we notice that the function that is returned 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. For example:
def count (): FS = [] for I in range (1, 4): Def f (): Return i*i fs.append (f) return FSF1, F2, F3 = count ()
In this example, each loop creates a new function, and then returns the three functions that were created.
You might think that calling F1 (), F2 (), 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 three 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 I have to use 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, to bind to the value of the function parameter 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) is executed immediately, so the current value of I is passed in F () return FS
And look at the results:
>>> 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.
Try Lambda:
def count (): Def f (j): Return Lambda:j*j # How does it feel meaningless? fs = [] for I in range (1, 4): Fs.append (f (i) ) # f (i) is executed immediately, so the current value of I is passed in F () return FS
The functional programming of Python self-study notes 5--return function