python3.x anonymous function lambda

# Anonymous function
lambda arguments: expression
keyword lambda indicates that it is an anonymous function, colon: in front of the variable is an argument of the anonymous function, after the colon is the return value of the function, pay attention here without using the return keyword.

  • ambda just an expression, function body is much simpler than def.
  • Lambda expression is a body, instead of a code block. We can only package a limited logic into the lambda expression.
  • lambda function has its own namespace, and can not be accessed outside of its own argument list or the global namespace parameters.
  • Although lambda function looks can only write a single line, but not equivalent to C or C ++ inline functions, which aims not take up the stack memory when calling small functions to increase operating efficiency.

Example: Contrast is very simple cpp

list_str=['test', None, '', 'str', ' ', 'END']

filter = Data (the lambda STR: STR and len (str.strip ())> 0, list_str)
Print ( 'Data:', Data, 'List (Data):', List (Data))
# character is why the filter output string list

map = map_data (the lambda STR: STR and len (str.strip ())> 0, list_str)
Print ( 'map_data:', map_data, 'List (map_data):', List (map_data))
# why the map is here It is of type bool list

 

But above there is unknown, not str bool type of list or why the map is returned

Guess you like

Origin www.cnblogs.com/liuruoqian/p/11309639.html