"Turn" python Learning (-python) function (ii)-About lambda

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/BeginMan/p/3178103.html

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 ():'beginman#python single line parameter can be written in one line with title     
Thelambda 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:

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.

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+Lambda x,y:x+y#3def sum (x,y=10):return x+Lambda x,y=10:x+ y#one#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 ():'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.

Iv. references

Http://www.cnblogs.com/coderzh/archive/2010/04/30/python-cookbook-lambda.html

Http://www.cnblogs.com/wanpython/archive/2010/11/01/1865919.html

"Turn" python Learning (-python) function (ii)-About lambda

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.