python 高阶函数

#简单的实例
def add(x,y,f):
return f(x)+f(y)

var = add(5,-9,abs)
print(var)

1.把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码的情况下为其添加功能)
2.返回值中包含函数名(不能修改函数的调用方式)

import  time

#对应1
def bar():
time.sleep(2)
print('in the bar')

def test2(func):
start_time = time.time()
func()
stop_time = time.time()
print('the func run time is %s'%(stop_time-start_time))

test2(bar)


#对应2
def bar():
time.sleep(2)
print('in the bar')

def test1(func):
print(func)
return func

bar = test1(bar)
bar()
原文地址:https://www.cnblogs.com/anhao-world/p/12879546.html