python嵌套函数和高阶函数:
1.嵌套函数:
#函数嵌套:在一个函数的函数体内用def声明,不加叫调用
def grandpa():
x=1
def dad():
x=2
def son():
x=3
print(x)
son()
dad()
grandpa()
2.高阶函数
2.1定义:将一个函数作为变量传给另一个函数:
import time
def bar():
time.sleep(2)
print('in the bar')
def test(func):
print(func)#打印了内存地址
return func
#首次调用
bar()
#在不影响调用的情况下,添加新功能
bar=test(bar)
2.2高阶函数示例:
import time
def timer(func):
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
print('func执行时间%s'%(stop_time-start_time))
return deco
@timer #test=timer(test)
def test():
time.sleep(2)
print('in the test')
@timer
def test2(name,age):
print('test2:',name,age)
test()
test2('wuyifan',23)
标签:函数,python,print,嵌套,func,time,test,def
来源: https://blog.csdn.net/qfqf123456/article/details/111773738