1. Anonymous function (lambda function): A lambda function is a minimal function that quickly defines a single line, borrowed from Lisp and can be used wherever a function is needed.
The normal function is defined as follows:
def f (x, y ): return X*y
F (2,3)
The lambda function is defined as follows:
g=Lambda x,y:x*y
G (2,3)
2. Benefits of using lambda functions
(1) When using Python to write some execution scripts, using lambda eliminates the process of defining a function and makes the code more streamlined.
(2) for some abstract functions that are not reused elsewhere, sometimes it is difficult to name a function, and a lambda function does not need to consider a naming problem.
(3) using a lambda function makes the code easier to understand at some point.
3. Reduce function: Reduce (F,L): The F function is applied to the sequence of L.
Reduce (lambda x,y:x*y,range (1,6)# The result is 1*2*3*4*5=120
anonymous function of Python learning