A lambda function is also called an anonymous function, that is, the function does not have a specific name. Let's look at an example:
def f (x): Return x**2print F (4)
The use of lambda in Python is written like this:
g = Lambda X:x**2print g (4)
So what is the use of lambda expressions?
Lambda and normal functions, is to omit the function name, while such an anonymous function, and can not be shared in other places called. In fact, it's true that Lambda does not play a real earth-shattering role in Python's dynamic language, because there are many other ways to replace Lambda. However, when using Python to write scripts, using lambda can eliminate the process of fixing the function, making the code more streamlined, and for some abstractions, functions that are not reused elsewhere, sometimes giving a name to a function is a challenge, and using lambda does not have to consider naming problems. , using lambda makes the code easier to understand at some point.
Lambda Basics
In a lambda statement, the colon is preceded by a parameter, which can have multiple, separated by commas, and the return value to the right of the colon. The lambda statement constructs a function object, for example:
g = Lambda X:x**2print g<function <lambda> at 0x00afaaf0>
Python lambda expression