python-Anonymous functions

Source: Internet
Author: User

in the Python language, in addition to the DEF statement used to define functions, you can also use anonymous function lambda, which is a python form of an expression that generates a Function object. An anonymous function typically creates a function that can be called, returns a function, and does not name the function. Lambda is sometimes called an anonymous function, which is why it takes a function and doesn't want to think about the name, which is the anonymous function.

Cases:

# this piece of code def Calc (x):     return x**xprint(calc# replaced by anonymous function  Lambda x x**xprint(Calc (10))

Role:

Lambda is an expression, not a statement

Because Lambda is an expression, it can appear in the Python language where the DEF statement cannot appear, and the lambda is compared to the DEF statement, which must name the new function at the beginning, and the former returns a new function that can have a selective assignment variable name .

A lambda body is just a single expression, and a block of code.

The lambda and common function functions define methods to compare it's functionality to be smaller, it's just an object that serves simple functions, and DEF handles larger data tasks.

Why use lambda?

It's good to answer this question with a paragraph from the Python Learning Handbook "Lambda has the function of sketching functions, allowing the definition of a function to be embedded within the code used. A more concise code structure can be created with only a small piece of executable code to embed. "The use of lambda is met in Python basics learning, but when it comes to Python's advanced stage, you need to do more in-depth learning."

python-Anonymous functions

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.