python 19 lambda function

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

A, lambda functions

1, lambda function basis:

lambda function, also called anonymous functions, ie functions no specific name, but the method is created def name. as follows:

"" "Named foo function" ""
def foo (): return 'beginman' #Python single-line parameters can be written on one line and title
"" "Create an anonymous function lambda keyword, the above expression with function" ""
lambda:'beginman'             

The above is simply a function object created with lambda, and did not save it nor to call it, the time will be recovered. Here we save and recall:

bar = lambda:'beginman'
print bar()      #beginman

From the above examples, the syntax can be easily understood Python lambda:

lambda [arg1[,arg2,arg3....argN]]:expression

lambda statements, the first parameter is the colon, can have multiple, separated by commas, the return value of the right side of the colon. lambda statement is actually constructed a function object.

print lambda:'beginman'   #<function <lambda> at 0x00B00A30>

2, no parameters

If there is no parameter, lambda front no colon, in the example above.

3, there are parameters

Copy the code
def add(x,y):return x+y
add2 = lambda x,y:x+y
print add2(1,2)     #3

def sum(x,y=10):return x+y
sum2 = lambda x,y=10:x+y
print sum2(1)       #11
print sum2(1,100)   #101
Copy the code

Two, lambda and def

In the above example, we can see a lambda is just create a simple function object is a single-line version of the function, but this statement for performance reasons, when the call stack bypass function assignment. What python lambda and def not the same?

1, python lambda creates a function object, but will not assign a function object identifier, and the assignment will def function object to a variable.

Such as:

>>> def foo():return 'foo()'
>>> foo
<function foo at 0x011A34F0>

2, python lambda it's just an expression, but it is def a statement. lambda expression as a function up and running, create a frame object when called.

Third, the use of lambda functions

Personally think the following:

1, for a one-way function, the process of using lambda-defined functions can save, make the code more streamlined.

2, in the case of non-function of multiple calls, lambda expression that is vested with improved performance

Note: If for..in..if can do, it is best not to choose lambda

Fourth, the reference

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

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

A, lambda functions

1, lambda function basis:

lambda function, also called anonymous functions, ie functions no specific name, but the method is created def name. as follows:

"" "Named foo function" ""
def foo (): return 'beginman' #Python single-line parameters can be written on one line and title
"" "Create an anonymous function lambda keyword, the above expression with function" ""
lambda:'beginman'             

The above is simply a function object created with lambda, and did not save it nor to call it, the time will be recovered. Here we save and recall:

bar = lambda:'beginman'
print bar()      #beginman

From the above examples, the syntax can be easily understood Python lambda:

lambda [arg1[,arg2,arg3....argN]]:expression

lambda statements, the first parameter is the colon, can have multiple, separated by commas, the return value of the right side of the colon. lambda statement is actually constructed a function object.

print lambda:'beginman'   #<function <lambda> at 0x00B00A30>

2, no parameters

If there is no parameter, lambda front no colon, in the example above.

3, there are parameters

Copy the code
def add(x,y):return x+y
add2 = lambda x,y:x+y
print add2(1,2)     #3

def sum(x,y=10):return x+y
sum2 = lambda x,y=10:x+y
print sum2(1)       #11
print sum2(1,100)   #101
Copy the code

Two, lambda and def

In the above example, we can see a lambda is just create a simple function object is a single-line version of the function, but this statement for performance reasons, when the call stack bypass function assignment. What python lambda and def not the same?

1, python lambda creates a function object, but will not assign a function object identifier, and the assignment will def function object to a variable.

Such as:

>>> def foo():return 'foo()'
>>> foo
<function foo at 0x011A34F0>

2, python lambda it's just an expression, but it is def a statement. lambda expression as a function up and running, create a frame object when called.

Third, the use of lambda functions

Personally think the following:

1, for a one-way function, the process of using lambda-defined functions can save, make the code more streamlined.

2, in the case of non-function of multiple calls, lambda expression that is vested with improved performance

Note: If for..in..if can do, it is best not to choose lambda

Fourth, the reference

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

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

Guess you like

Origin www.cnblogs.com/cmybky/p/11772407.html