Python's lambda function

Source: Internet
Author: User



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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.