python's lambda

Then python, there is a cross-lambda things, also known as anonymous functions, it tries to explain what the specific use.

Look at this piece of code:

>>> def add(x):
...     return x+1
...
>>> add(5)
6
>>> add(7)
8
>>>

 

Here, a simple definition of a function, the function name is add, a function of one argument x, the function is the function and return x + 1, then, using lambda to achieve the same functionality.

>>> g = lambda x: x+1
>>> g(1)
2
>>> g(2)
3
>>>

 

Since the anonymous lambda function is called, then it would still ultimately a function, but does not require time-defined function name (function must have a name when using def defined functions), Note: The front of the equal sign here is not the name of the function g , but the anonymous function definitions you assign g, g is a variable, a colon (:) in front of the anonymous function parameter, followed by the function of the anonymous function.

*************** short step a thousand miles ***************

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11938316.html