Lambda in Python and Pythonlambda

Source: Internet
Author: User

Lambda in Python and Pythonlambda

Lambda x: x * x is actually

Def f (x ):

Retrun x * x

The keyword lambda indicates an anonymous function, and the x before the colon indicates a function parameter.

An anonymous function can have only one expression, and no return is required. The returned value 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 (5)

25

Similarly, an anonymous function can be used as a return value, for example:

Def build (x, y ):

Return lambda: x * x + y * y

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.