Using lambda to create anonymous functions, and the method created with Def is named, but what else does Python lambda have to do with def other than the method name on the surface?
1 Python lambda creates a function object, but does not assign the function object to an identifier, and Def assigns the function object to a variable.
2 Python Lambda It's just an expression, and DEF is a statement.
If you use Python lambda in the Python list parsing, I don't feel much of a thing because Python lambda creates a function object, but discards it immediately, because you're not using its return value, which is the function object. It is also because Lambda is just an expression that can be directly a member of a Python list or a Python dictionary, such as:
info = [Lambda a:a**3, Lambda b:b**3]
There is no way to replace the DEF statement directly in this place. Because DEF is a statement, not an expression that cannot be nested inside, a lambda expression can have only one expression after ":". That is, in Def, return can also be placed behind a lambda, and cannot be returned with return can not be defined behind a Python lambda. Therefore, a statement such as if or for or print cannot be used in a lambda, and lambda is generally used only to define simple functions.
Iii. use of lambda functions
1. For single-line functions, using lambda eliminates the process of defining a function, making the code more streamlined.
2. In the case of functions that are not called multiple times, lambda expressions are used to improve performance
3. 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.
Lambda exists to reduce the definition of a single-line function.
The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.
A lambda expression acts as a function sketch. Allows the definition of a function to be embedded within the code.
--------------------------------------------------------------
Lambda syntax: Lambda [arg1[,arg2,arg3....argn]]:expression
1. For a single parameter:
g = Lambda x:x*2
Print g (3)
The result is 6.
2. Multiple parameters:
m = lambda x, y, Z: (x-y) *z
Print m (3,1,2)
The result is 4.
3. No parameters
f = lambda:'beginman'
F () #beginman
4. Lambda used in filter, map, reduce function.
Print Range (6) # 0,1,2,3,4,5
Print Range (1,6) # 1,2,3,4,5
Print reduce (lambda x,y:x*y, Range (1,6)) #1 *2*3*4*5
5. Lambda can is used in common Def fucntion.
def action (x):
Return Lambda Y:x+y
F = Action (2) # This is a lambda object
Print F (3) # 5
--------------------------------------------------------------------------------------------------------------- ----------------------
A Python lambda expression