Python lambda expression

Source: Internet
Author: User

A lambda function is also called an anonymous function, that is, the function does not have a specific name. Let's look at an example:

def f (x): Return x**2print F (4)
The use of lambda in Python is written like this:
g = Lambda X:x**2print g (4)

So what is the use of lambda expressions?

Lambda and normal functions, is to omit the function name, while such an anonymous function, and can not be shared in other places called. In fact, it's true that Lambda does not play a real earth-shattering role in Python's dynamic language, because there are many other ways to replace Lambda. However, when using Python to write scripts, using lambda can eliminate the process of fixing the function, making the code more streamlined, and for some abstractions, functions that are not reused elsewhere, sometimes giving a name to a function is a challenge, and using lambda does not have to consider naming problems. , using lambda makes the code easier to understand at some point.

Lambda Basics

In a lambda statement, the colon is preceded by a parameter, which can have multiple, separated by commas, and the return value to the right of the colon. The lambda statement constructs a function object, for example:

g = Lambda X:x**2print g<function <lambda> at 0x00afaaf0>

Python lambda expression

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.