|
The Python lambda keyword can be understood as: its function is similar to function pointers. Official translation lambda is an anonymous function, which is relatively normal function is as an illustration:
Define a normal function, to achieve operation by 1:
def plus1 (x):
return x + 1
The above statements are implemented:
1. Define a function, the function is named: plus1
2. This function has one argument
Corresponding anonymous function statement writing:
lambda x: x + 1
Note that this is an expression, so he actually can not do anything. . .
So if we want to call the function to achieve operational by 1, respectively, by way of example for normal function and the anonymous function is as follows:
Function to achieve real name:
def plus1 (x):
return x + 1
a = 0
a = plus1 (a)
print a
Anonymous functions to achieve:
func = lambda x: x + 1
a = 0
a = func (a)
print a
Conclusion usage anonymous function both as a C language macro definitions, like a C language function pointer.
The anonymous function and branded functions in combination even more fun, such as:
def plus1 (x):
return x + 1
func = lambda x: plus1 (x)
a = 0
a = func (a)
print a
You see, this is not a function pointer usage yet?
With the C language function pointer becomes extremely flexible, too, will spend after lambda, python can also become equally flexible. |
|
|
|