Python Lambda Introduction

Source: Internet
Author: User
Tags readable

In the process of learning Python, the syntax of Lambda is often confusing, what is lambda, why use lambda, is it necessary to use lambda?

Here are answers to the above questions.

1. What is lambda?

Look at an example:

g = Lambda x:x+1

Take a 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 thought that lambda, as an expression, defines an anonymous function, the code x for the example above is the entry parameter, x+1 is the function body, and is represented by a function:

2     return x+1

Very easy to understand, where Lambda simplifies the writing of function definitions. Code is more concise, but the use of functions is more intuitive and easy to understand.

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

Print filter (printmap (lambda x, y:x + y, foo) 139  

The role of the map in the example above is very simple and clear. But does Python have to use lambda to make this simple? In the object traversal process, in fact Python for. In.. The IF syntax is already strong and is more readable than lambda.

For example, the map above can be written as:

print [x * 2 + ten for x in Foo]

It's very concise and easy to understand.

The filter example can be written as:

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

It's also easier to understand than a lambda way.

Here's a brief description of what Lambda is, and here's why you use lambda to see an example (from apihelper.py):

and (or (lambda s:s) 

  In Visual Basic, you are most likely to create a function that takes a string argument and a collapse parameter, and uses the if statement to determine whether to compress the white space before returning the corresponding value. This approach is inefficient because the function may need to handle every possible situation. Every time you call it, it will have to decide if you want to compress the blanks before giving them what you want. In Python, you can take the decision logic out of the function and define a cut-off Lambda function to provide the exact (unique) you want. This approach is more efficient, more elegant, and rarely leads to bugs that are annoying (oh, think those parameters are dizzy).

Using this example, we found that the use of lambda is a lot easier to simplify the code and make the code concise and clear. However, it is important to note that this reduces the readability of the code to some extent. People who are not very familiar with Python may find this incomprehensible.

Lambda defines an anonymous function

Lambda does not increase the efficiency of the program, only makes the code more concise.

If you can use for...in ... If to complete, be resolute without lambda.

If you do not include loops within LAMBDA,LAMBDA, if so, I would rather define functions to do so that the code is reusable and better readable.

Summary: Lambda exists to reduce the definition of a single-line function.

Python Lambda Introduction

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.