Python lambda function

Source: Internet
Author: User

The Lambda keyword in python to define a lambda anonymous function. Form such as: Lambda parameter: expression. Lambda requires a parameter, followed only by a single expression as the body of the function, and the value of the expression is returned by the new function.

Lambda functions have the following characteristics compared to DEF-defined functions :

  1. Lambda functions are anonymous functions, and DEF defines functions that are known as functions. Lambda creates a function object, but does not assign the function object to an identifier, and Def assigns the function object to a variable.

  2. A lambda function is a single-line function. The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.

  3. The lambda function, like the function defined by Def, is a Python object.

  4. The expression part of a lambda function can only be an expression, not a statement, so a statement like if or for or print cannot be used in a lambda.

Example:

A=lambda:3print A () B=lambda X:x*2print B (2) C=lambda x,y:x+yprint C (2,3)

Operation Result:

3

4

5


Python lambda function

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.