Home > Backend Development > Python Tutorial > What is an anonymous function? What are the uses of Python anonymous functions?

What is an anonymous function? What are the uses of Python anonymous functions?

Tomorin
Release: 2018-08-17 17:54:45
Original
7009 people have browsed it

When we pass in a function, sometimes there is no need to explicitly define the function. It is more convenient to pass in the anonymous function directly.

In Python, there is limited support for anonymous functions. Still taking the map() function as an example, when calculating f(x)=x2, in addition to defining a function of f(x), you can also directly pass in an anonymous function:

>>> list(map(lambda x: x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9]))
[1, 4, 9, 16, 25, 36, 49, 64, 81]
Copy after login

As can be seen from comparison, Anonymous function lambda x: x * x is actually:

def f(x):
    return x * x
Copy after login

The keyword lambda represents an anonymous function, and the x before the colon represents the function parameter.

Anonymous functions have a limitation, that is, they can only have one expression. There is no need to write return. The return value is the result of the expression.

There is an advantage to using anonymous functions, because the function has no name, so you don’t have to worry about function name conflicts. In addition, the anonymous function is also a function object. You can also assign the anonymous function to a variable and then use the variable to call the function:

>>> f = lambda x: x * x
>>> f
<function <lambda> at 0x101c6ef28>
>>> f(5)25
Copy after login

Similarly, you can also return the anonymous function as a return value, such as:

def build(x, y):
    return lambda: x * x + y * y
Copy after login

Encapsulates details to improve security and controllability. It is often used outside functions in the global scope, thereby limiting the addition of too many variables and functions to the global scope.
Using block-level scope in the global scope can reduce the problem of closures occupying memory, because there is no reference to the anonymous function, and its scope chain can be destroyed immediately as long as the function is executed.

Imitate block-level (private) scope:

function box(){
    for(var i=0;i<5;i++){  //块级作用域(js无)
    }   
     var i //即便重新声明,也不会影响之前的值
    alert(i);//5
 }
box();
Copy after login


The above is the detailed content of What is an anonymous function? What are the uses of Python anonymous functions?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template