Python higher-order function basics eleven: higher-order functions and actual parameters without parameters

1. A function is a variable
. The definition of a function in python means a variable whose type is function, and the function name is the name of the
variable. Anything a variable can do, a function can do

Variables as the parameters of the sequence Variables as the parameters
of the function
Variables as the return value of the
function Note: The function body will not be executed when it is defined

list1 = []
for x in range(0, 5):
    list1.append(lambda y: y * x)

print(list1[2](3))
print(list1[4](3))

x = 0 [lambda y: y x]
x = 1 [lambda y: y
x], [lambda y: y x]
x = 2 [lambda y: y
x], [lambda y: y x], [lambda y : y x]
x = 3 [lambda y: y x], [lambda y: y x], [lambda y: y x], [lambda y: y x]
x = 4 [lambda y: y x], [ lambda y: y x], [lambda y: y x], [lambda y: y x], [lambda y: y x]
执行 第二 个 [lambda y: y
x] , y 为 3 , x 为 4 值为 12
执行 第四 个 [lambda y: y * x] , y 为 3 , x 为 4 值为 12

1. Actual parameter higher-order function The function that the
parameter is a function is the actual parameter higher-order function
2. The use of the system's common actual parameter higher-order functions
max, min, sorted, map, reduce
1) max\min
max (sequence)-comparison sequence The size of the element is used to obtain the maximum value. Element
max (sequence, key = function)-the function determines the comparison object when seeking the maximum value. Requirement for
parameter key: a. is a function b. This function has one and only one parameter (parameter in the sequence)
c. The function has a return value (comparison object)

students = [
    {'name': '小明', 'age': 23, 'score': 90},
    {'name': '张三', 'age': 30, 'score': 78},
    {'name': '小红', 'age': 18, 'score': 82},
    {'name': 'Tom', 'age': 26, 'score': 99}
]
# 求所有学生中成绩最好发学生
print(max(students, key=lambda item: item['score']))


def f2(item):
    print(f'item:{item}')
    return item['score']


print(max(students, key=f2))

2) sorted
sorted (sequence)-compare the size of the elements in the sequence, sort the elements in the sequence from small to large
sorted (sequence, key = function)-compare the size of the comparison object when the function determines the sort

students = [
    {'name': '小明', 'age': 23, 'score': 90},
    {'name': '张三', 'age': 30, 'score': 78},
    {'name': '小红', 'age': 18, 'score': 82},
    {'name': 'Tom', 'age': 26, 'score': 99}
]
print(sorted(students, key=lambda item: item['age']))


def a(x):
    return x['age']


print(sorted(students, key=a))

3) map
map (function, sequence)-transform the sequence in the specified way, the return value is a map object (map object is a sequence)
function: 1. There is a parameter (this parameter points to the element in the original sequence) 2. Need a return value (element in the new sequence)

nums = [19, 78, 76, 55, 30, 12]
result = map(lambda item: item % 10, nums)
print(list(result))

# 练习:获取班级所有学生的总成绩
c = [90, 89, 88, 99, 27, 76, 56]
e = [98, 78, 66, 82, 90, 12, 78]
m = [23, 98, 100, 72, 69, 27, 96]
result = map(lambda item1, item2, item3: item1 + item2 + item3, c, e, m)
print(list(result))

4) Reduce performs a common operation on all data
from functools import reduce. To add the requirement for calling
reduce (function, sequence, initial value)
function: a. Two parameters are required: the
first parameter-the first point to the initial Value; starting from the second time, it points to the result of the previous operation. The
second parameter-points to each element in the sequence.
b. A return value is required (to determine the operation rule)

# 将数字序列中的元素全部拼接在一起
# [10, 89, 78, 20] -> 10897820
nums = [10, 89, 78, 20]
result = reduce(lambda s,item:s+str(item),nums,'')
print(result)

1. What is a decorator?
The role of a decorator: used to add functions to a function.
Essence: it is a function (real parameter higher-order function + return value higher-order function + sugar syntax)
Usage (routine):
def function name 1 (parameter 1 ):
def function name 2 (*args, **kwarg):
the code of the new function
Call the original function code: parameter 1 (*args, **kwarg)
return the return value of the original function
return function name 2
description:
function name 1-The name of the decorator, which is named according to the newly added function.
Parameter 1:-The function that needs to be added (the original function), generally named as f, fn
function name 2-The function name of the new function after the function is added, nuw_f, new_fn

Guess you like

Origin blog.csdn.net/SaharaLater/article/details/111564657