Reviewing Python today and seeing an example of a lambda function, using lambda in Python is handy at some point because you don't have to create a new function to implement some simple functionality. But there is a lambda instance that makes me wonder, now put it out and speculate on its function, if there are errors, please correct me!
1 def func(a,b,operation):
2 print("a = %d"%a)
3 print("b = %d"%b)
4 print("result =",operation(a,b))
5 func(11,22,lambda x, y:x + y)
6 print("------------------------")
7 func(11,22,lambda x, y : x - y)
Operation Result:
Visible: By passing three parameters to the Func () function, in order: 11,22,lambda function, which means lambda x, y:x + y is assigned to operation, then operation = lambda x, y:x + y. The third statement in the Func () function, print ("result=", operation), calls operation, which is equivalent to filling the lambda function in place of the operation, The X and y of two values in the operation brackets are assigned to the lambda, and the addition of A and B is achieved through the lambda function, which is 11 + 22.
Here operation = Lambda x, y:x + y, operation is equivalent to invoking the instance name of a lambda function, assigning a value to a function instance is equivalent to passing a value to the lambda function, the result of the lambda expression is similar to the return expression, but the lambda The function can pass the value of the expression back to the call without returning it, and the function example uses the operation instance variable to invoke the lambda function, then the value of the lambda expression is returned to Operation (), noting that it is not returned to the operation variable!!! Operation is similar to the function name of an anonymous function, the output function name cannot get the returned value, only use the function name () to output the result.
func = lambda x : x + 2
print(func)
print(func(3))
As can be seen, the func here is equivalent to the function name of lambda, although lambda is an anonymous function, but calling this anonymous function requires an instance, and this func is his instance.
However, anonymous functions do not need a function name, which is purely superfluous, and is rarely used in Python programming.
Python's lambda function