【Python3】高阶函数

满足其一即是

  1. 函数名作实参传给函数(不修改被修饰函数源代码的情况下为其添加功能)
  2. 返回值含函数名(不修改函数调用方式)
def add(x,y,f):
    return f(x) + f(y)
 
res = add(3,-6,abs)
print(res)
#1
import
time def suspend(): time.sleep(3) print('延迟三秒后输出这句话) def count_time(func) start_time=time.time() func() stop_time=time.time() print('the time is %s'%(start_time-stop_time)) count_time(suspend)
#2
import
time def suspend(): time.sleep(3) print('延迟三秒后输出这句话) def count_time(func) print(func) #打印内存地址 return func #返回内存地址 func_new=count_time(suspend) #返回的内存地址赋值给func_new func_new() #运行suspend
原文地址:https://www.cnblogs.com/shengxinjack/p/7743591.html