Python Learning Note 7: Function objects and function objects as parameters

Source: Internet
Author: User

First, the lambda function
Like what:
Fun1 = lambda x,y:x + yprint fun1 (3,4)
Output: 7

Lambda generates a function object. The function 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 code is equivalent to:
def fun2 (x, y):    return x + y

Second, function as the number of parameters
function can act as an object. To pass the number of parameters.
Like what:
Fun = lambda x, Y:x+ydef runfun (fun, A, b): "Print Fun"    (b) runfun (fun, 3, 5)
Output: 8

Another example:
Runfun ((lambda x,y:x**2 + y), 6, 9)

C. Map () function
Map () is a python built-in function. Its first parameter is a function object.


The function of map () is to apply function objects to each element of the table in turn. and returns the result.


Such as:

Ret1 = Map ((lambda x:x+1), []) Print Ret1ret2 = map (lambda x,y:x+y), [1,2,3],[10,100,1000]) print Ret2
Output:
[2, 3, 4] [11, 102, 1003]

Third, filter () function
The first parameter of the filter function is also a function object. It is also the function object that will act as a parameter to multiple elements.
Assuming that 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.
Such as:
Def fun3 (a):    if a >:        return True    else:        return falseprint filter (FUN3, [10,20,100,110])
Output: [+]


Four, 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 of the parameters.


Like what:

Print reduce ((lambda x,y:x+y), [1,2,3,4,5])
Output: the

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 (3) acts as the second parameter of the lambda function, making the next call to the lambda function, resulting in 6.


Call the lambda function in turn. The first parameter of each lambda function is the result of the previous operation, and the second is the next element in the table, until there are no remaining elements in the table.


The example above. Equivalent (((1+2) +3) +4) +5


Note: In the 3.x version number map,filter,reduce These three functions are slightly different

Python Learning Note 7: Function objects and function objects as parameters

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.