SoFunction
Updated on 2024-11-16

Analyzing python lambda expression forms

Preface:

In the course of daily development, sometimes you will temporarily use some simple functions, their business logic will be relatively simple, so simple that it is not worth to let them leave their names, this time, it is worth using anonymous functionslambdafunction to fulfill this requirement.

lambda expression format

lambda arguments : statement

expression starts with​ lambda​Keyword start, colon ":" left is the function's incoming parameters, when there is more than one incoming parameter use the comma to divide, the right side of the colon is the return value of the expression statement, the function will be based on the expression to calculate the result and will return it. ​lambda​An expression creates a function object that can be assigned a value and used as a normal function. The following defines a lambda expression for squaring:

>>> lambda x : x * x
<function <lambda> at 0x000001C98ED8E040>

Where x is the argument to the function and the expression after the colon is the return value of the function, you can tell at a glance that the function is just asking for the square of that variable, but as a function, how do you use it without a name?
Here we temporarily bind a name to the anonymous function, which makes it possible to call the anonymous function.

>>> square = lambda x : x * x
>>> square
<function <lambda> at 0x000001C98F24B040>
>>> square(8)
64

It is equivalent to the regular function

>>> def square(x: int) -> int:
... return x * x
...
>>> square
<function square at 0x000001C98ED8E040>
>>> square(8)
64

With this example it is clear to observe the difference in presentation between a lambda expression and an ordinary function expression. A lambda declaration is recorded as a lambda in the python VM, whereas an ordinary function will be barred with the name of the function directly reflected. Imagine that when an exception occurs.​Traceback​It prints the exception message, but doesn't mark the exact location, and doesn't tell you which function is in trouble, so the efficiency of troubleshooting will be greatly reduced.

>>> div1 = lambda x : 1 / x
>>> div1(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
ZeroDivisionError: division by zero
>>> def div2(x: int) ->int:
... return 1 / 0
...
>>> div2(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in div2
ZeroDivisionError: division by zero

As shown in the code, for div1, an exception occurs, which tells you that you wrote a certainlambdaThe function has an exception and it's on the first line of it, but then it doesn't tell you which function :), but for div2 it's pretty straightforward to state that thediv2It's easy to locate the second line of a lambda that's gone wrong. Here's the intuitive hint that writing a lambda can't be too complicated, and that you should do your best to keep it simple and error-free, and that if there's a chance that an exception might occur, you might as well just write it as a normal function. This is what Effective Python advocates:Use helper functions to replace complex expressions and give functions clear names to improve code readability.

Usage of Anonymous

insofar aslambdais more often used in higher-order functions, passing itself as an argument to a higher-order function, such as in the case ofmapfilter cap (a poem)reduce functions that take a function as a parameter, creating anonymous functions using lambda expressions is the best scenario if you don't want to define additional functions.

>>> list(map(lambda x : x * x, [1, 2, 3, 4, 5, 6, 7, 8]))
[1, 4, 9, 16, 25, 36, 49, 64]
>>> list(filter(lambda x : x < 2, [1, 2, 3, 4, 5, 6, 7, 8]))
[1]
>>> reduce(lambda x, y: x + y, [1, 2, 3, 4, 5, 6, 7, 8])
36

python lmabda 形式分析_lambda表达式

This article on python lambda expression form analysis is introduced to this article, more related python lambda content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!