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
- 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:
- Visible to local variables
- Visible to global variables
- Visible to parameters passed in the current layer
- Visible to parameters passed in the upper function
- Visible to the parameters passed in by the upper lambda
Lambda Evaluation Benefits
- 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.
- Can be defined within a method, which improves ease of operation
Disadvantages
- Lambda is an anonymous function, because it is anonymous, so the readability becomes worse.
- 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