The usage of lambda in Python and its difference with def, pythonlambda

Source: Internet
Author: User
Tags python list

The usage of lambda in Python and its difference with def, pythonlambda

Lambda in python is usually used to create anonymous functions in python. The method created with def has a name, except that the method names on the surface are different, lambda in python is different from def in the following aspects:

1. python lambda creates a function object, but does not assign this function object to an identifier, while def assigns the function object to a variable.

2. python lambda is just an expression, while def is a statement.

The following is the python lambda format, which looks very simplified.

lambda x: print x

If you use python lambda in python list parsing, it doesn't make much sense, because python lambda creates a function object, but it is discarded immediately, because you didn't use its return value, that is, the function object. Lambda is just an expression that can be directly used as a member of the python list or python dictionary, for example:

info = [lamba a: a**3, lambda b: b**3]

There is no way to directly replace it with the def statement. Because def is a statement, not an expression that cannot be nested in it, lambda expressions can only have one expression after. That is to say, in def, return can be returned or placed behind lambda. return cannot be used or defined behind python lambda. Therefore, statements such as if, for, or print cannot be used in lambda. lambda is generally only used to define simple functions.

The following is an example of python lambda:

1. single parameter:

g = lambda x*2print g(3)

The running result is 6.

2. Multiple parameters:

m = lambda x,y,z: (x-y)*zprint m(3,1,2)

The running result is 4.


What is lambda in Python? How can it be used?

Lambda is an anonymous function, that is, a function without a name. It is simple and practical. It comes from the concept of functional programming (this does not know Google). Even Java 7 seems to have added this...

For example
The general function is as follows:

Def f (x ):
Return x + 1

Print f (4)

If lambda is used, it is written as follows:
G = lambda x: x + 1
Print g (4)

Python lambda usage

The value returned when question is used is bool.
When the value is 1, the system calls the exit function. When the value is 0, a default function is called.
Lambda: None
Is an anonymous function that does nothing.
In fact, you do not need to write it like this. I think the author wants to increase the complexity of the Code.
Can be written
If button:
Self. quit ()
 

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.