Python Advanced Tutorial Function object (function is also object) _python

Source: Internet
Author: User
Tags in python

Adhering to the concept of all objects, we look back again at functions (function). A function is also an object that has attributes (you can use the Dir () query). As an object, it can also be assigned to another object name, or passed as a parameter.

Lambda functions

Before we start, we'll mention the lambda function. You can use the syntax of a lambda function to define a function. The lambda example is as follows:

Copy Code code as follows:

Func = lambda x,y:x + y
Print func (3,4)

The 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. Func calls are no different from normal functions.

The above definition can be written in the following form:

Copy Code code as follows:

def func (x, y):
return x + y

function as a parameter to pass

A function can be passed as an object for parameter passing. The name of the function (such as Func) is the object. For example:

Copy Code code as follows:

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. Passing Func to F () in F,test has the function of func ().

We can therefore increase the flexibility of the program. You can use the test function above to bring in different function parameters. Like what:

Copy Code code as follows:

Test (lambda x,y:x**2 + y), 6, 9)

Map () function

Map () is a built-in function of Python. Its first argument is a function object.

Copy Code code as follows:

Re = map ((lambda x:x+3), [1,3,5,6])

Here, the map () has two parameters, one is the function object defined by the lambda, and the other is a table containing more than one element. The function of map () is to function objects in turn to each element of the table, and the result of each action is stored in the returned table re. The map operates on the data by reading the function (here is the lambda function) (where "data" is every element of the table, and "Operation" is 3 for each data).

In Python 3.X, the return value of the map () is a loop object. The list () function can be used to convert the loop object into a table.

If a function object that is a parameter has more than one argument, you can pass more than one parameter of a function parameter to the map () by using the following method:

Copy Code code as follows:

Re = map ((lambda x,y:x+y), [1,2,3],[6,7,9])

Map () takes one element out of each of the two tables each time, bringing it into the function defined by the lambda.

Filter () function

The first parameter of the filter function is also a function object. It also acts on multiple elements as a function object that is a parameter. If the function object returns True, the element of that time is stored in the returned table. Filter filters data by reading the function. Similarly, in Python 3.X, filter returns not a table, but a loop object.

The filter function is used in the following example:

Copy Code code as follows:

def func (a):
If a > 100:
Return True
Else
Return False

Print 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 move a function to various parameters in a progressive manner. The following example:

Copy Code code as follows:

Print reduce ((lambda x,y:x+y), [1,2,5,7,9])

The first parameter of reduce is a lambda function that receives two parameter x,y and returns X+Y.

Reduce passes the first two elements (1 and 2) in the table to the lambda function, and gets 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, and the next call to the lambda function is 8. Call the lambda function in turn, and the first parameter of the lambda function is the result of the previous operation, and the second parameter is the next element in the table until there are no remaining elements in the table.

Example above, 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, you need to introduce the package, see the comment area.

Summarize

function is an object

To define a function with a lambda

Map ()

Filter ()

Reduce ()

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.