Transferred from: http://www.cnblogs.com/vamei/archive/2012/07/10/2582772.html
Adhering to the concept of all objects, we look back at functions. The 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, and the return value is x+y. The function object is assigned to Func. The invocation of Func is the same as a 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. The function name (such as Func) is the object. For example:
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,3,5,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 sequentially on 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 (where "data" is each element of the table, and "operation" is 3 per 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 pass multiple parameters of the function parameter to map () using the following method:
Re = map ((lambda x,y:x+y), [1,2,3],[6,7,9])
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. It is also a function object that will act as a parameter to multiple elements. If the function object returns True, the elements of that secondary are stored in the returned table. Filter filters the data by a 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 >: return True else: return falseprint filter (func,[10,56,101,500])
Reduce () function
The first parameter of the reduce function is also a function, but there is a requirement that the function itself can receive two parameters. Reduce can progressively function on individual parameters. The following example:
Print reduce ((lambda x,y:x+y), [1,2,5,7,9])
The first parameter of reduce is a lambda function, which receives two parameters x, Y, and returns X+Y.
Reduce passes the first two elements (1 and 2) in a table to a lambda function, resulting in 3. The return value (3) is used as the first parameter of the lambda function, and the next element in the table (5) acts as the second parameter of the lambda function, making the next call to the lambda function, resulting in 8. Call the lambda function in turn, each time the first parameter of the lambda function is the result of the last operation, and the second argument is the next element in the table, until there are no remaining elements in the table.
The above example, equivalent (((1+2) +5) +7) +9
According to Mmufhy's reminder: the reduce () function is not directly used in 3.0, it is defined in the Functools package, need to introduce the package, see the comment area.
Summarize
A function is an object
Defining a function with lambda
Map ()
Filter ()
Reduce ()
Python--function object