Introduction to anonymous function lambda in Python

Source: Internet
Author: User
Tags anonymous

In the process of learning Python, the syntax of the lambda is often confusing, what the lambda is, why the lambda is used, and whether the lambda must be used.

Let's answer the questions above.

1. What is a lambda?

Look at an example:

1 g = lambda x:x+1

Look at the results of the execution:

G (1)

>>>2

G (2)

>>>3

Of course, you can also use this:

Lambda x:x+1 (1)

>>>2

It can be argued that, as an expression, a lambda defines an anonymous function, and the example code x is the entry parameter, x+1 is the function body, and is represented by a function:

1 def g (x):
2 return x+1

Very easy to understand, where Lambda simplifies the writing form of function definitions. The code is more concise, but the use of the definition of a function is more intuitive and easy to understand.

In Python, there are several well-defined global functions that are handy to use, filter, map, and reduce

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> Print filter (lambda x:x% 3 = 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> Print Map (Lambda x:x * 2 +, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce (lambda x, y:x + y, foo)
139

The role of the map in the example above is very simple and clear. But does Python need to use a lambda to be so neat? In terms of object traversal processing, in fact python for ... In.. The IF syntax is already strong, and it trumps lambda in easy reading.

Examples of the above map can be written as:

print [x * 2 + for x in Foo]

Very concise, easy to understand.

The example of filter can be written as:

print [x for x in foo if x% 3 = 0]

It is also easier to understand than the way of a lambda.

It briefly describes what a lambda is, and here's why you use lambda to see an example (from apihelper.py):

Processfunc = Collapse and (lambda s: "". Join (S.split ())) or (lambda s:s)

In Visual Basic, you are likely to create a function that accepts a string parameter and a collapse parameter, and uses an if statement to determine whether to compress whitespace and then return the corresponding value. This approach is inefficient because the function may need to deal with every possible situation. Every time you call it, it will have to decide whether to compress the blanks before giving you what you want. In Python, you can take decision logic out of the function, and define a reduced lambda function to provide the exact (unique) you want. This approach is more efficient, more elegant, and rarely causes bugs that are annoying (oh, thinking about those parameters).

In this example, we find that the use of lambda simplifies the code and makes the code concise and clear. However, it is important to note that this will reduce the readability of the code to some extent. People who are not very familiar with Python may find this incomprehensible.

The lambda defines an anonymous function

The lambda does not increase the efficiency of the program's operation, but only makes the code more concise.

If you can use for...in ... If to complete, resolutely do not use a lambda.

If you use LAMBDA,LAMBDA not to include loops, if so, I'd rather define a function to do so that the code gets reusable and better readable.

Summary: Lambda exists to reduce the definition of one-line functions.

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.