Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the format of the python lambda expression

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the format of python lambda expression". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Foreword:

In the daily development process, sometimes some simple functions are temporarily used, and their business logic will be relatively simple, so simple that it is not worth leaving their names. At this time, it is worthwhile to use the anonymous function lambda function to complete this requirement.

Lambda expression format lambda arguments: statement

The expression begins with the lambda keyword, and the left side of the colon ":" is the incoming parameter of the function, separated by a comma when there are multiple input parameters, and to the right of the colon is the expression statement that returns the value. The function evaluates the result based on the expression and returns it. The lambda expression creates a function object that can be assigned and used like a normal function. A lambda expression squared is defined below:

> lambda x: X * x

Where x is the parameter of the function, and the expression after the colon is the return value of the function. You can see at a glance that this function is finding the square of the variable, but as a function, how to use it without a name?

Here we will bind a name to the anonymous function for a moment, which makes it possible for us to call the anonymous function.

> square = lambda x: X * x > square > square (8) 64

It is equivalent to a regular function.

> def square (x: int)-> int:... Return x * x... > > square > square (8) 64

Through this example, you can clearly observe the difference in performance between lambda expressions and ordinary function expressions. After the lambda declaration, it is recorded as lambda in the python virtual machine, while the ordinary function will directly reflect the function name. Imagine that when an exception occurs, Traceback prints the exception information, but does not mark the exact location or tell you which function is wrong, so the troubleshooting efficiency will be greatly reduced.

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

As the code shows, for div1, there is an exception, which tells you that one of the lambda functions you wrote has an exception, and it is in its first line, but it will not tell you which function:), but for div2, it will directly explain that there is something wrong with the second line of div2, which is easy to locate. Well, this also intuitively implies to us that writing lambda should not be too complicated, and we should try our best to make it concise and never make mistakes. If it is possible that an exception may occur, it would be better to write it as an ordinary function. This is what the above "Effective Python" advocates: use helper functions to replace complex expressions and give functions a clear name to improve code readability.

Use of anonymity

For lambda, it is more often used in higher-order functions, passing itself as parameters to higher-order functions, such as map, filter and reduce functions, which accept a function as a parameter. If you do not want to define additional functions, then using lambda expressions to create anonymous functions is the best application scenario.

> 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

This is the end of the content of "what is the format of the python lambda expression". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report