Python-Functional programming

Source: Internet
Author: User

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Function object

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.

1. Lambda function

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:

lambda x,y: x + yprint 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
2. function as 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)
3. 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:

map((lambda x,y: x+y),[1,2,3],[6,7,9])map()将每次从两个表中分别取出一个元素,带入lambda所定义的函数。
4. 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 > 100: return True else: return Falseprint filter(func,[10,56,101,500])
5. 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

Warning: the reduce () function is not directly used in 3.0, it is defined in the Functools package and needs to be introduced into the package.

Python-Functional programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.