Python function (1) lambda

Source: Internet
Author: User

First, the lambda function

1. Basic lambda function:

The lambda function is also called an anonymous function, that is, the function does not have a specific name, and the method created with Def is a name. As follows:

"" "named Foo function" "Def foo (): Return ' Beginman '  #Python中单行参数可以和标题写在一行
The Lambda keyword creates an anonymous function that is the same as the function "" "Lambda: ' Beginman '             

The above simply creates a function object with lambda, does not save it and does not call it, and is recycled at all times. Here we save and invoke:

Bar = lambda: ' Beginman ' Print bar ()      #beginman

The Python lambda syntax is easy to understand from the above examples:

Lambda [arg1[,arg2,arg3....argn]]:expression

In a lambda statement, the colon is preceded by a parameter, which can have multiple, separated by commas, and a return value to the right of the colon. A lambda statement is actually built as a function object.

Print lambda: ' Beginman '   #<function <lambda> at 0x00b00a30>

2, no parameters

If there are no parameters, the lambda Colon does not precede it, as in the above example.

3, have parameters

def add (x, y): return x+yadd2 = lambda x,y:x+yprint add2 (     #3def) sum (x,y=10): return x+ysum2 = lambda x,y=10:x+yprint SUM2 (1)       #11print sum2 (1,100)   #101

Second, Lambda and def

In the above example, the lambda function simply creates a simple function object, which is a single-line version of a function, but the statement is called to bypass the stack allocation of the function for performance reasons. What else does Python lambda have to do with Def?

1. Python lambda creates a function object, but does not assign the function object to an identifier, and Def assigns the function object to a variable.

Such as:

>>> def foo (): Return ' foo () ' >>> foo<function foo at 0x011a34f0>

2, Python Lambda It's just an expression, and DEF is a statement. A lambda expression runs like a function when it is called to create a Frame object.

Iii. use of lambda functions

Personal opinion has the following:

1. For single-line functions, using lambda can eliminate the process of defining a function, making the code more streamlined.

2. In the case of functions that are not called multiple times, lambda expressions are used to improve performance

Note: if for. In.. If you can do it, it's best not to choose lambda.

Python function (1) lambda

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.