Python lambda expression

Source: Internet
Author: User
Tags for in range

Recently found in coding use lambda still have many advantages, many times the code is more neat, more pythonic, so briefly summarize here

What is 1.LAMBDA?

To give a simple example:

Lambda x:x*xdef  func (x):    return x*x

The definition of two Func is exactly the same, and the two function definition methods are used in conjunction with map to square all the elements in the list and what the code will look like.

def func (x):     return x* for in range) map (Lambda-in Range (10)])

In contrast, the effect is obvious. The function of the Func function is very simple and probably only used this time, so here we define a function that is simple and less frequently used. In this example, using lambda to create an anonymous function does not affect the readability of the code, but it also streamlines the code and reduces unnecessary function calls. In fact, this kind of scene is very common, we need a simple single-line function, do a simple thing, we even don't care about the name of the function, when Lambda is our good choice.

2. Whether to use lambda

Lambda defines an anonymous function that does not lead to an increase in the efficiency of code execution. Lambda is often used in conjunction with Map,reduce,filter when traversing lists, but the pursuit of lambda is often disastrous in terms of code readability. Python has strict constraints on lambda, after all, it can only be made up of one expression. Lambda is convenient and not fake, but if used excessively, the logic of the program does not look so clear, after all, everyone's understanding of the abstract is different.

If a list is generated, it can be implemented using only for,if,in, and I won't use lambda

If the function is not simple enough to involve complex logic such as loops, I will define the function to make the code more readable and I will not use lambda at this time

In my opinion, the existence of lambda is to reduce the definition of single-line function, so it is sufficient to replace the definition of one-line function.

Python lambda expression

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.