Lambda function (lambda)
Single line of expression of a function.
A function can be define in 2 way:
defkeywords, which can define afunction that has their own name.lambdakeywords, which is use to define aanonymous function.
- A define function that can
use many time as they have their own name.- While anonymouse function
only can use once / temporaily
Syntax
py
lambda params1, params2 : single_line_of_function_expressionInfo
- lambda function only suitable for the scenario that only use the function once. If he function that required to use many time, then define function would be the best choice.
Usage
lambda.py
- Based on the example of Function as a parameters / arguments,
lambda function also able to replace a define function.
py
## Function as a params (lambda)
def addition_operation(compute):
results = compute(1, 2)
print(type(compute))
print(results)
addition_operation(lambda x, y: x + y)Output:
<class 'function'>
3