A Python lambda expression

Source: Internet
Author: User
Tags python list

Using lambda to create anonymous functions, and the method created with Def is named, but what else does Python lambda have to do with def other than the method name on the surface?

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.
2 Python Lambda It's just an expression, and DEF is a statement.

If you use Python lambda in the Python list parsing, I don't feel much of a thing because Python lambda creates a function object, but discards it immediately, because you're not using its return value, which is the function object. It is also because Lambda is just an expression that can be directly a member of a Python list or a Python dictionary, such as:

info = [Lambda a:a**3, Lambda b:b**3]

There is no way to replace the DEF statement directly in this place. Because DEF is a statement, not an expression that cannot be nested inside, a lambda expression can have only one expression after ":". That is, in Def, return can also be placed behind a lambda, and cannot be returned with return can not be defined behind a Python lambda. Therefore, a statement such as if or for or print cannot be used in a lambda, and lambda is generally used only to define simple functions.

Iii. use of lambda functions

1. For single-line functions, using lambda eliminates 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

3. If you can use for...in ... If to complete, be resolute without lambda.

If you do not include loops within LAMBDA,LAMBDA, if so, I would rather define functions to do so that the code is reusable and better readable.

Lambda exists to reduce the definition of a single-line function.

The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.

A lambda expression acts as a function sketch. Allows the definition of a function to be embedded within the code.

--------------------------------------------------------------

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


1. For a single parameter:
g = Lambda x:x*2
Print g (3)
The result is 6.

2. Multiple parameters:
m = lambda x, y, Z: (x-y) *z
Print m (3,1,2)
The result is 4.

3. No parameters

f = lambda:'beginman'

F () #beginman

4. Lambda used in filter, map, reduce function.

Print Range (6) # 0,1,2,3,4,5
Print Range (1,6) # 1,2,3,4,5
Print reduce (lambda x,y:x*y, Range (1,6)) #1 *2*3*4*5

5. Lambda can is used in common Def fucntion.

def action (x):
Return Lambda Y:x+y

F = Action (2) # This is a lambda object
Print F (3) # 5

--------------------------------------------------------------------------------------------------------------- ----------------------

A Python lambda expression

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.