Anonymous function lambda
Python uses the lambda keyword to create anonymous functions. By anonymous, I mean that a function is no longer defined in the standard form of a def statement. The purpose of such statements is to bypass the function's stack allocation at call time for performance reasons. The syntax is:
lambda [arg1[, arg2, ... argN]]: expression
where parameters are optional and usually also appear in the expression if they are used.
The following example illustrates the use of lambda statements (without parameters).
# Ways to define a function using def def true(): return True # Equivalent lambda expressions >>> lambda :True <function <lambda> at 0x0000000001E42518> # Keep lambda objects in variables to be called at any time >>> true = lambda :True >>> true() True
Here's another example with parameters.
# Functions defined using def def add( x, y ): return x + y # Expressions using lambda lambda x, y: x + y # lambda also allows default values and the use of variable-length arguments lambda x, y = 2: x + y lambda *z: z # Calling lambda functions >>> a = lambda x, y: x + y >>> a( 1, 3 ) 4 >>> b = lambda x, y = 2: x + y >>> b( 1 ) 3 >>> b( 1, 3 ) 4 >>> c = lambda *z: z >>> c( 10, 'test') (10, 'test')
However, due to the special design of lambda expressions, it also brings some different experiences in specific scenarios.
A lambda is an expression, not a statement This allows it to appear in places where def can't, for example, in list constants.
lambda is a single expression, not a block of code lambda is designed to meet the scenarios of simple functions, can only encapsulate a limited amount of logic, there are complex logic of the situation has def to deal with, so the function of lambda is much smaller than def.
Also, lambda expressions can be nested
>>> action = (lambda x : (lambda y : x + y)) >>> a = action(10) >>> a(5) 15
This is a closure implemented in lambda, and like a normal closure, the embedded lambda expression gets the variables of the higher-level lambda function.
Use of Anonymous Functions
Anonymous functions are often used as arguments to higher-order functions (higher-order functions, functions whose arguments are functions). For example, several built-in functions: filter (), map (), reduce (). Below we look at the use of these functions and another python feature to achieve the same effect of the use of
filter function
>>> list = [1, 2, 3] >>> result = filter(lambda x: x%2==0, list) >>> result [2] >>> result = [x for x in list if x%2==0] >>> result [2]
map function
>>> result = map(lambda x: x*2, list) >>> result [2, 4, 6] >>> result = [x*2 for x in list] >>> result [2, 4, 6]
reduce function
>>> result = reduce(lambda x, y: x+y,list) >>> result 6 >>> result = sum(list) >>> result 6
With the exception of the reduce function, which has a special alternative usage, the map and filter functions can be replaced by list comprehension. It is said that lambda was added to python by a Lisp programmer back in the day, and Guido was strongly against it, favoring list comprehension.
Jump table
Another use of lambda is to write jump tables, which are lists or dictionaries of behaviors that perform specific actions on demand.
>>> key = "get" >>> {"abc":(lambda : 2 + 2),"bcd" : (lambda : 3 + 3), "get" : (lambda : 4 + 4)}[key]() 8
This leaves each lambda in the dictionary with a function that can be called subsequently, which can be taken out and called by indexing. This allows fields to become more generalized tools for multiple branching.
The above is a small introduction to the Python anonymous function (lambda function) detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. I would also like to thank you very much for your support of my website!