The so-called return function, as the name suggests, is to use the function as the return value. In addition to taking functions as parameters, higher-order functions can also return functions as results. Let's implement a continuous multiplication of variable parameters. The quadrature function can be defined as:
def calc_fac(*args): fx = 0 for n in args: fx = fx * n return fx
Many times when the quadrature function is defined, we don't need to multiply it immediately, but will calculate it based on subsequent calculations. line call. At this time, we can not return the result of the function, but return the function itself, as shown below:
def lazy_fac(*args): def fac(): fx = 0 for n in args: fx = fx * n return fx return fac
After redefining the quadrature function, when we call lazy_fac(), what is returned is not the function The result of the product is the product, but the product function:
>>> a=lazy_fac(1,2,3,4) >>> a <function lazy_fac.<locals>.fac at 0x002a5dr42>
And when function a is called, the product process is executed:
>>> a() 24
In this example, we define it in the function lazy_fac function fac, fac can be called an internal function, and lazy_fac is an external function. The internal function fac can refer to the parameters and local variables of the external function. Parameters and variables are saved in the inner function fac we finally return. This program structure is called a closure in Python.
In the closure structure, when the function returns as a result, the function process is not executed immediately, but is executed after we call a().
In short, in functional programming, in addition to returning a calculation result, a function can also return an unexecuted function. When returning a function, always remember that the function has not been executed, and try not to introduce variables such as loop variables that may cause changes in the returned function.
When we pass in a function or the code structure is too complex, we can use anonymous functions instead of explicit function definitions. At this time, anonymous functions will greatly simplify the code structure and make it more refined.
Take the map function introduced before as an example to calculate x3 and look at the functions of the anonymous function:
>>> list(map(lambda x: x * x * x, [1, 2, 3])) [1, 8, 27]
As can be seen from the comparison, the anonymous function lambda x: x*x*x is Equivalent to:
def f(x): return x * x * x
lambda is used as the keyword of the anonymous function, and the x before the colon indicates the parameters of the anonymous function. Anonymous functions can only have one expression, and there is no need to write return like defining a function. Because anonymous functions do not have function names, there is no need to worry about function name conflicts during use. As a function object, the anonymous function also conforms to the rules of assigning values to variables:
>>> fx = lambda x: x * x * x >>> fx <function <lambda> at 0x101c6ef28> >>> f(4) 64
Similarly, the anonymous function as a function can also be used as a return function:
def f(x): return lambda: x * x * x
The above is the detailed content of Python functional programming: returning functions and anonymous functions. For more information, please follow other related articles on the PHP Chinese website!