Adhering to the concept of all objects, we look back at functions. A function is also an object with attributes (you can use the Dir () query). As an object, it can also be assigned to other object names or passed as parameters.
Lambda functions
Before we start, let's mention the lambda function. You can use the syntax of a lambda function to define a function. The lambda example is as follows:
Func = Lambda x,y:x+y
Print func (3,4)
Lambda generates a function object. The function parameter is x, Y, the return value is x+y, and the function object assigns to func.func the same call as the normal function.
The above definitions can be written in the following form:
def func (x, y):
return x + y
function as a parameter pass
Functions can be used as an object for parameter passing. function name (e.g. func)
def test (F,A,B):
print ' Test '
Print F (A, B)
Test (func,3,5)
The first argument f of the test function is a function object. The ability to pass Func to F () in F,test has the function of func ().
We can therefore improve the flexibility of the program. You can use the test function above to bring in different function parameters. Like what:
Test ((Lambda x,y:x**2 +y), 6,9)
Map () function
Map () is a python built-in function. Its first argument is a function object.
Re = map ((lambda x:x+3), [1,2,3,6])
Here, map () has two parameters, one is a function object defined by lambda, and the other is a table with multiple elements. The function of map () is to function objects once with each element of the table, and the result of each action is stored in the returned table re. Map uses the read-in function (here is the lambda function) to manipulate the data
In Python 3.x, the return value of map () is a looping object. You can use the list () function to convert the loop object into a table.
If the function object as a parameter has more than one parameter, you can use the following method to first map () pass multiple parameters of the function parameter:
Re = map ((lambda x,y:x+y), [1,2,3],[6,7,8])
Map () takes one element from each of the two tables at a time and brings it into the function defined by the lambda.
Filter () function
The first parameter of the filter function is also a function object. He is also a function object that acts as a parameter to multiple objects. If the function object returns True, the element being changed is stored with the returned table, and filter filters the data by the function that is read in. Similarly, in Python 3.x, filter returns not a table, but a loop object.
The filter function uses the following example:
def func (a)
If a > 100:
Return True
Else
Return False
Print filter (func,[10,56,2333])
Reduce () function
One parameter of the reduce function is also a function, but there is a requirement that the function itself can receive two parameters, and that reduce can progressively function with each parameter, as in the following example:
Print reduce ((lambda x,y:x+y), [1,2,3,4,5])
The first parameter of reduce is the lambda function, which receives two parameters x, Y, and returns X+Y.
Reduce passes the first two elements (1 and 2) in the table to the lambda function, resulting in 3. The return value (3) will be the first parameter of the lambda function.
The next element in the table (5), as the second parameter of the lambda function, makes the next call to the LAMDBA function, resulting in 8. .....
Python Advanced 07 Function object