Tag: set return int [1] Cannot assign value expression Test Direct
Python Lambda
In Python, if you want to create a function using the keyword DEF, and if you want to create an anonymous function, you need to use lambda.
What is the difference between a function created by lambda and a function created by def?
- Def creates a function that has a name, and Lambda has no
- Lambda creates an object of a function, but does not give the object an identifier, and Def assigns the function object to a variable
DEF is a statement, and Lambda is just an expression
y = lambda x:print (x)
Y (10) # 10
For example, an expression is created with lambda, and X is equivalent to a formal parameter, and the code as a whole is very simple and efficient.
Typically, a lambda can be directly a member of a list or dictionary, as follows:
So the same operation, in the list, can only use lambda, there is no way to use def substitution, because DEF is a statement, in the list can only be an expression. A lambda expression can have only one expression after ":", usually the return statement can be returned to be placed behind a lambda, and cannot be returned after the same cannot be placed. Therefore, it is best not to put a statement such as if or for in the back of a lambda expression, and lambda is best used only to define simple functions.
Python Lambda Brief Introduction