Detailed Explanation of Anonymous Function of python Foundation

  • 2021-11-01 03:48:49
  • OfStack

Directory 1. Introduction to anonymous functions 2. Syntax 3. Use scenarios 4. Comparison between anonymous functions and ordinary functions 5. Various forms of anonymous functions 6. lambda is passed as a parameter 7. lambda functions are used in conjunction with python built-in functions 8. lambda is used as the return value of functions

1. Introduction to anonymous functions

Anonymous functions refer to 1 class of functions or subroutines that do not need to define identifiers. Python uses lambda syntax to define anonymous functions, using expressions without declarations.

In python, functions that are not declared by def but defined by the lambda keyword are called anonymous functions.

The lambda function can accept any number of parameters (which can be 0), but can only return the value of one expression. The lambda function is a function object, which is directly assigned to a variable, and this variable becomes a function object.

2. Grammar

lambda Parameter: Expression

3. Use scenarios

(1) When you need to pass a function object as a parameter, you can directly define an lambda function (as a parameter or return value of the function)

(2) The business to be processed conforms to the lambda function (any number of parameters and 1 return value), and this function will only be used in 1 place and will not be reused in other places, so the lambda function can be used

4. Comparison of anonymous functions and ordinary functions


def sum_func(a, b, c):
    return a + b + c
#  Assign an anonymous function object to  sum_lambda
sum_lambda = lambda a, b, c: a + b + c
print(sum_func(1, 2, 3))  # 6
print(sum_lambda(1, 2, 3))  # 6

5. Multiple forms of anonymous functions


#  Parametric-free 
lambda_a = lambda :100
print(lambda_a())  # 100
 
# 1 Parameters 
lambda_b = lambda num: num * 10
print(lambda_b(1))  # 10
 
#  Multiple parameters 
lambda_c = lambda a, b, c: a + b + c
print(lambda_c(1, 10, 100))  # 111
 
#  Expression branching 
lambda_d = lambda x: x if x > 5 else x + 1
print(lambda_d(4))  # 5
print(lambda_d(6))  # 6

6. lambda is passed as a parameter


def sub_func(a, b, func):
    print("a = ", a)
    print("b = ", b)
    print("a - b = ", func(a, b))
sub_func(3, 2, lambda a, b: a - b)
#  Results: 
#     a =  3
#     b =  2
#     a - b =  1

7. lambda functions are used in conjunction with python built-in functions

sorted is a built-in function for sorting lists in Python, and we use lambda to get the sorted key


member_list = [
    {"price": 9},
    {"price": 999},
    {"price": 99}
]
new_list = sorted(member_list, key=lambda dict_: dict_["price"])
print(new_list)  #  [{'price': 9}, {'price': 99}, {'price': 999}]

number_list = [100, 77, 69, 31, 44, 56]
num_sum = list(map(lambda x: {str(x): x}, number_list))
print(num_sum)  # [{'100': 100}, {'77': 77}, {'69': 69}, {'31': 31}, {'44': 44}, {'56': 56}]

map is a built-in function used for mapping in Python, which receives two parameters, the first parameter is a function, and the second parameter is an iterable object. map will traverse the values of iterable objects, and then pass the values to the functions for execution in turn. We use lambda to implement the function parameters in map.

8. lambda as the return value of the function


def discount_func(discount):
    return lambda price: discount * price
 
p = discount_func(0.8)
print(p)  # <function discount_func.<locals>.<lambda> at 0x00000241352BAC10>
print(p(100))  # 80.0

Anonymous function can be used as the return value of a function, the above function discount_func returns a discounted anonymous function object, call this object, pass in the price, you can get the discounted price


Related articles: