Python Basics-Lambda keyword

Source: Internet
Author: User

The Lambda keyword in Python can be understood as: It functions like a function pointer.

The official translation of Lambda is an anonymous function, which is relative to the normal function, for example:

Define a normal function that implements the increment 1 operation:

def plus1 (x):    return x+1

The above statement implements: 1. Defines a function called: PLUS12. This function has a parameter

The corresponding anonymous function statement writing:

Lambda x:x+1

Note that this is an expression, so he can't actually do anything ...


So if we want to invoke the function to implement the increment 1 operation, we use the normal function and the anonymous function for example, respectively, as follows:

Real-name function implementation:

def plus1 (x):    return x+1a = 0a = Plus1 (a) print a

anonymous function implementations:

Func = Lambda x:x+1a = 0a = Func (a) print a

Conclusion the use of anonymous functions, both as macro definitions in C and as function pointers in the C language


It's even more fun to combine anonymous and real-name functions, such as:

def plus1 (x):    return x+1func = lambda X:plus1 (x) a = 0a = Func (a) print a
you see, this is not the use of function pointers?


The C language has a function pointer that becomes infinitely more flexible, and as well, Python can become equally flexible once lambda is used.

Python Basics-Lambda keyword

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.