in the Python language, in addition to the DEF statement used to define functions, you can also use anonymous function lambda, which is a python form of an expression that generates a Function object. An anonymous function typically creates a function that can be called, returns a function, and does not name the function. Lambda is sometimes called an anonymous function, which is why it takes a function and doesn't want to think about the name, which is the anonymous function.
Cases:
# this piece of code def Calc (x): return x**xprint(calc# replaced by anonymous function Lambda x x**xprint(Calc (10))
Role:
Lambda is an expression, not a statement
Because Lambda is an expression, it can appear in the Python language where the DEF statement cannot appear, and the lambda is compared to the DEF statement, which must name the new function at the beginning, and the former returns a new function that can have a selective assignment variable name .
A lambda body is just a single expression, and a block of code.
The lambda and common function functions define methods to compare it's functionality to be smaller, it's just an object that serves simple functions, and DEF handles larger data tasks.
Why use lambda?
It's good to answer this question with a paragraph from the Python Learning Handbook "Lambda has the function of sketching functions, allowing the definition of a function to be embedded within the code used. A more concise code structure can be created with only a small piece of executable code to embed. "The use of lambda is met in Python basics learning, but when it comes to Python's advanced stage, you need to do more in-depth learning."
python-Anonymous functions