A brief introduction to Python's lambda anonymous functions

  • 2020-04-02 13:10:55
  • OfStack

Lambda functions are also called anonymous functions, that is, functions have no specific name. Let's take a look at the simplest example:


def f(x):
return x**2
print f(4)

If I use lambda in Python, I'll write it like this

g = lambda x : x**2
print g(4)

Lambda expressions have implementations in many programming languages. Such as c # :


var g = x => x**2
Console.WriteLine(g(4))

So what's the use of lambda expressions? Many people argue that lambda is nothing more than a function without the name, and that anonymous functions like this can't be Shared anywhere else. Indeed, lambda doesn't really set the world on fire in a dynamic language like Python, because there are many alternatives to lambda. At the same time, lambda notation sometimes doesn't seem quite as pythonic. It has even been suggested that a later Python version would cancel lambda.

Looking back, is a lambda in Python really useless? Not really. At least the points I can think of are:

1. When writing execution scripts in Python, using lambda can simplify the code by eliminating the need to define functions.

2. For some abstract functions that can't be reused anywhere else, sometimes it's difficult to give a name to a function, so lambda doesn't have to worry about naming.

3. Using lambda makes the code easier to understand at some point.

Lambda basis
In a lambda statement, the colon is preceded by an argument, which can be multiple, separated by a comma, and the return value to the right of the colon. The lambda statement builds a function object. Witness this:


g = lambda x : x**2
print g
<function <lambda> at 0x00AFAAF0>

Since C#3.0, lambda expressions have been added, eliminating the need to write delegates. The lambda expression keyword in C# is = > Here's an example:

var array = new int[] {2, 3, 5, 7, 9};
var result = array.Where(n => n > 3); // [5, 6, 9]


C# USES extension methods to make array objects have convenient methods like Where and Sum. In Python, there are several defined global functions that are easy to use: filter, map, reduce.

>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
139

Lambda?
The map in the above example is as simple and convenient as the Where extension to C#. But does Python have to use lambda to achieve this level of simplicity? In terms of object traversal processing, Python's for.. In.. The if syntax is already powerful and beats lambda in readability. For example, the example of map above can be written as:


print [x * 2 + 10 for x in foo]

Very simple and easy to understand. The example of filter can be written as:

print [x for x in foo if x % 3 == 0]

It's also easier to understand than the lambda way.

So, when to use lambda and when not to use it is a case by case matter, as long as the intent is clear. In general, if for.. In.. I wouldn't choose lambda if I could.

Lambda broken?
Lambda is used a lot in math teaching, and one of the guys had this problem. He wanted to create an array of functions fs=[f0... f9] where fi(n)= I +n.


fs = [(lambda n: i + n) for i in range(10)]

But, oddly enough,

>>> fs[3](4)
13
>>> fs[4](4)
13
>>> fs[5](4)
13

The result did not live up to the man's expectations. The expected result should be:

>>> fs[3](4)
7
>>> fs[4](4)
8
>>> fs[5](4)
9

The problem is with the variable I. The above code is replaced with a simple scaled-down version that does not use lambda:

i = 1
def fs(n):
return n + i
print fs(1) # 2
i = 2
print fs(1) # 3

As you can see, the reason this didn't work out is that the I in lambda USES a global variable other than an anonymous function. Revise it:

fs = [(lambda n, i=i : i + n) for i in range(10)]
>>> fs[3](4)
7
>>> fs[4](4)
8
>>> fs[5](4)
9


Related articles: