Customizing usage scenarios for lambda functions in Python programming

Source: Internet
Author: User

There are two ways to define a function in Python, one defined by the normal definition of Def, a function to specify a name, and the second to be defined with a lambda, without specifying a name, called a lambda function.

Lambda function is also called anonymous function, anonymous function is no Name function, function does not have a name? Yes, of course. Some functions, if only temporary, and its business logic is very simple, there is no need to give it a name.

Like the film of the mass actors, often they play a little, the most is to foil starring, running extras, they need a name? No, because they just temporary out of the mirror, the next time may not need, so don't bother to give each of them a name, after all, take an elegant name is very laborious thing.

Let's start with a simple lambda function.

>>> Lambda x, y:x+y <function <lambda> at 0x102bc1c80>

X and Y are the two parameters of the function, the expression after the colon is the return value of the function, you can see that this function is to ask for two variables of the and, but as a function, no name how to use it? Here we give this anonymous function a name, which makes it possible for us to invoke the anonymous function

>>> add = lambda x, y:x+y >>> add <function <lambda> at 0x102bc2140> >>> Add (1 , 2) 3

It is equivalent to a regular function

>>> def add2 (x, y): ... return x+y ... >>> add2 <function add2 at 0x102bc1c80> >>> a DD2 (3)

If you define an anonymous function and you want to bind a name to it, it's a bit superfluous, usually using a lambda function directly. So where is the correct usage scenario for the LAMDBA function?

1. Function-Type programming

Although Python is not a purely functional programming language, it provides many of the features of functional programming, such as map, reduce, filter, sorted, which support functions as parameters, and lambda functions can be applied in functional programming.

Read the question: A list of integers that asks for an ascending order of the absolute size of the elements in the list, what would you do? Think for a minute and look down.

>>> List1 = [3,5,-4,-1,0,-2,-6] >>> sorted (List1, Key=lambda x:abs (x)) [0,-1,-2, 3,-4, 5,-6]

The sort function sorted supports receiving a function as a parameter, which is ordered as sorted, which is sorted by the absolute value of the list element, and of course I can do it with a normal function:

>>> def foo (x): ... return abs (x) ... >>> sorted (list1, Key=foo) [0,-1,-2, 3,-4, 5,-6]

It's just this way the code doesn't look pythonic enough.

2. Closed Package

The closure itself is an obscure concept, it can be specifically used in a single article to introduce, but here we can be easily and rudely understood as a closure is a function defined inside the function, closure so that the variable even out of scope of the function can still be accessed.

Consider an example of using a lambda function as a closure.

>>> def my_add (n): ... return lambda x:x+n ... >>> add_3 = My_add (3) >>> Add_3 (7) 10

The lambda function here is a closure, in the global scope, add_3 (7) can be executed normally and the return value is 10, the reason is to return 10 because in the My_add local scope, the value of the variable N in the closure of the role of the function so that it can be accessed in the global scope.

Switching to a regular function can also implement closures, but this approach is slightly verbose.

>>> def my_add (n): ... def wrapper (x): ... return x+n ... return wrapper ... >>> add_5 = My_add (5) >>> Add_5 (2) 7

So is it any case that lambda functions are clearer than regular functions? See this example:

f = lambda x: [[Y for J, y in Enumerate (set (x)) if (i >> j) & 1] for I in range (2**len (set (x)))]

This is a lambda function that returns all the subsets of a collection, do you understand? It's hard for me to see it.

Zen of Python has such a word that Explicit is better than implicit (clearer than obscure). Remember, if you don't use a lambda function to make your code clearer, you should consider defining the function in a regular way.

Source: Technology House

Customizing usage scenarios for lambda functions in Python 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.