Deep understanding of Lambda

Source: Internet
Author: User

Overview

A lambda is an expression, or it can be said to be an anonymous function. However, it is not so easy to use it or to read the lambda code. Because it is anonymous because it has omitted some necessary explanatory information (such as the method name). Let's talk about how Lambda works and transforms.

Copyright notice

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Coding-naga
Published: March 10, 2016
Links: http://blog.csdn.net/lemon_tree12138/article/details/50774827
Source: CSDN
More content: Category >> thinking in Python

Directory

    • Overview
    • Copyright notice
    • Directory
    • Lambda
      • Preliminary understanding
        • Expressions and Definitions
        • Example description
      • Variable Scope description
      • Lambda reviews
        • Advantages
        • Disadvantages
    • Ref

Lambda preliminary understanding expressions and definitions

Lambda [arg1 [Arg2, Arg3, ... ArgN]: expression
– "Python core programming"

The description of the lambda expression above is included in the book "Python Core Programming". That is, in the equation to the right of the lambda, the left side of the colon is the parameter value, and the right is the evaluation expression.

Example description

1. Single-layer parameter summation
Now suppose you need to sum the two numbers. For normal logic code, it is not difficult to write the following code:

def sum(x, y):    return x + y

And in lambda, we can write this:

lambda x, y: x + y

The code is a lot more concise, but because of the lack of a method name to describe this step, we have some questions about the lambda expression that sums it up, which is what it is intended for in the program, which we cannot speculate on.

2. Nesting parameter summation
Demand is still in demand, but we have changed the transfer of parameters. What is called a nested parameter, we can refer to the following code:
Note: The following piece of code is actually a syntax error, and the purpose of this code is simply to illustrate the problem. Do not imitate

def sum_outer(x=0):    def sum_inner(y):        return x + y

The following code is used to rewrite the lambda:

def test_lamdba2(x=0):    returnlambda y: x + y

The conversion relationship between the two code logic is as follows:

3. Nesting lambda
A scenario in which a method is nested internally is assumed. We say that Lambda is understood to be an "expression method" nested inside a method. Therefore, you can also be able to do a layer of lambda to rewrite the method. Rewrite the method test_lambda2 in your code into a lambda. The rewritten description is as follows:

An analysis of the conversion process shown is performed with the previous transformation, and a conclusion can be drawn that in the lambda expression, the preceding lambda is the outer method, and the subsequent lambda is the secondary outer method, and is recursively pushed from the outside in the second.

Variable Scope description

About the scope of the variable in lambda from the above pictures can also be seen thinking, the main can make the following summary:

    1. Visible to local variables
    2. Visible to global variables
    3. Visible to parameters passed in the current layer
    4. Visible to parameters passed in the upper function
    5. Visible to the parameters passed in by the upper lambda
Lambda Evaluation Benefits
    1. In the normal code a few lines of code, in the lambda only need a line to solve. So the code is more concise than before.
    2. Can be defined within a method, which improves ease of operation
Disadvantages
    1. Lambda is an anonymous function, because it is anonymous, so the readability becomes worse.
    2. Sometimes multiple lambda nesting (just like the 3rd in an instance) makes the program difficult to understand
Ref
    • "Python core Programming"
    • The Python Learning Handbook

Deep understanding of Lambda

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.