lambda function is how to use it? Used in what position in what areas can achieve the effect we want to use it? So convenient function there has been talk about you know what? Here we take a look.
Brief introduction
lambda function called anonymous function, with the difference that he is generally a function, do not charge the name of God to think of a function, this is the biggest difference between the two! Therefore, under certain circumstances, particularly convenient to use.
lambda expression:
lambda arg1, arg2, … : expression
Using lambda keyword defined above;
parameters arg1, arg2, etc. represents a list of one or more parameters, the brackets do not need to pay attention;
colon: indicates a new block of statements is not;
the expression: expression is a parameter list parameter calculated
Note: no retrun return, the entire expression is an anonymous function return value;
anonymous function can only be written on one line, also known as one-way function; anonymous function need not be present if while for other statements;
lambda of scenarios:
The lambda function assigned to a variable;
Define a multiplication function, and the value to the variable MUL, can perform mul (1,2);
The lambda functions assigned to other functions;
The standard library sleep time functions as shield, as long as the program is called when the initial law;
the time.sleep the lambda = X: None, sleep time library function does not perform the original function;
The other functions as a lambda function return value;
The lambda functions as arguments passed to other functions (map, filter, reduce);
See two complex Liezi:
With the new sequence after mapping function map output sequence processing.
Using the filter function of the filter outputs odd 0-9;