Python Interview-lambda

Source: Internet
Author: User

The lambda function in Python is also called an anonymous function, that is, there is no specific name. The subject of a lambda is an expression, not a block of code, that encapsulates only a limited amount of logic in a lambda expression.


Let's compare the normal definition of a function:

def f (x):    return x**2print F (4) # result16

Use Lambda to write this:

g = Lambda X:x**2print g (4) # result16

For lambda expressions, the function name is omitted from the normal function, and such anonymous functions cannot be shared elsewhere. In short, Lambda does not actually play a critical role, and there are many ways to use it instead of lambda.

The most important role:

1. Using Python to write some execution scripts, you can use lambda to omit the process of writing a definition function, making the code more streamlined.

2. For some abstractions that do not use functions that are reused elsewhere, it is difficult to name a method, and you can use lambda without having to consider naming the problem.

3. Use lambda to make your code easier to understand at some point.

4. Lambda creates a function object, but does not assign the function object to an identifier, and the definition function assigns the function object to a variable.



Lambda Basics

In a lambda statement, the colon is preceded by a parameter, can have multiple, separated by commas, and the right side of the colon is the return value.

g = lambda X:x**2print g# result<function <lambda> at 0x0000000001fa7358>

Lambda and def work together

def action (x):    return lambda Y:x+ya = action (2) print a (+) # Result24

You first define an action function that returns an expression of a lambda. The lambda expression obtains the value of the variable name x in the scope of the upper Def function.

A is the return value of the action function, that is, the lambda expression

A (22), that is, the return lambda expression that called the action


If it's all written in Lambda,

b = lambda X:lambda Y:x+ya = B (2) Print Aprint A ($) # result<function <lambda> at 0x0000000001f673c8>24


Python has many well-defined global functions that can be used in conjunction with Lambda

foo = [2, 9, 8, 27]print filter (lambda x:x% 3 = = 0, foo) print map (Lambda x:x * 2 + ten, foo) print re Duce (lambda x, y:x + y, foo) # result[18, 9, 24, 12, 27][14, 46, 28, 54, 44, 58, 26, 34, 64]139


For the above example of map, we do not have to use lambda, can be written as:

print [x * 2 + ten for x in Foo]

The filter example can be written as:

print [x for x in foo if x% 3 = = 0]

Using the list comprehension is much easier to understand than lambda.



The concept of global variables in lambda

Create an array of functions fs = [F0,..., F9] where fi (n) = i + N, so:

FS = [(lambda n:i + N) for I in range (10)]

However, it does not get the desired result:

>>> Fs[3] (4) # It should be 713>>> Fs[4] (4) # 813>>> Fs[5] (4) # 913

Passing in 4 to the returned lambda expression, and the result is 13, then infer that the lambda expression is lambda N:9 + N, and the problem is above I.

The code version without lambda should look like this:

i = 1def FS (n):    return n + iprint FS (1) # 2i = 2print FS (1) # 3

Here I is a global variable, so we need to modify the initial code so that I in the lambda expression is a global variable.

FS = [(lambda n, i=i:i + N) for I in range (]>>> fs[3] (4) 7>>> Fs[4] (4) 8>>> fs[5] (4) 9



Reference:

http://blog.csdn.net/xiaobei4929/article/details/10530723

Http://www.cnblogs.com/qq78292959/archive/2013/05/07/3064438.html

http://math.andrej.com/2009/04/09/pythons-lambda-is-broken/

Http://www.secnetix.de/olli/Python/lambda_functions.hawk

Python Interview-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.