Basic python Tutorial-anonymous function lambda

Source: Internet
Author: User
This article mainly introduces the python basic tutorial about anonymous function lambda. For more information, see Python lambda

When using a function, sometimes you do not need to define a function that is displayed. you can use an anonymous function to make it more convenient and support anonymous functions in Python.

For example, if we want to calculate two numbers a and B, that is, f (a, B) = a + B. We can do this in two ways. The first one is to define a function f (x, y), and then pass the parameter to get the result. The second method is to use an anonymous function.

f = lambda x,y:x+y >>>f(1,2) 3

The anonymous function lambda x, y: x + y is actually:

def f(x, y):   return x + y

In python, the keyword lambda indicates an anonymous function, and x and y in front of the colon indicates the function parameters. the syntax of an anonymous function is:

lambda [arg1[,arg2,arg3....argN]]:expression

In a lambda statement, there are multiple parameters before the colon, which are separated by commas. the result of the expression on the right of the colon is used as the return value of the anonymous function.

An anonymous function has a limit that only one expression is allowed. return is not required. The return value of an anonymous function is the result of this expression. There is a benefit to using an anonymous function because the function has no name and you don't have to worry about function name conflicts. In addition, an anonymous function is also a function object. you can assign an anonymous function to a variable and use the variable to call the function:

>>> f = lambda x: x * x >>> f 
 
   at 0x101c6ef28> >>> f(5) 25
 

You can also use an anonymous function as the return value of the function, for example:

def build(x, y):   return lambda: x + y

Thank you for reading this article. I hope it will help you. thank you for your support for this site!

For more articles about anonymous function lambda in the basic python tutorial, refer to the PHP Chinese website!

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.