python-装饰(高阶函数)

高阶函数

1、把一个函数名当做实参传给另外一个函数(在不修改被装饰函数源代码)

2、返回值 中包含函数名

高阶函数实现1的功能

def bar():
print("in the bar")
def test1(func):
print("in the test1")
print(func)
func() #func=bar func()=bar()
test1(bar) 打印结果
---------------------------------

in the test1
<function bar at 0x000001DA445500D0>
in the bar

#实现bar的运行时间的高阶函数
import time
def bar():
time.sleep(3)
print("in the bar")
def test1(func):
star_time=time.time()
func() #bar()
stop_time=time.time()
print("the func run time is %s"%(stop_time-star_time))
test1(bar) 打印结果
-----------------------------------
in the bar
the func run time is 3.0002150535583496

第二个功能实现

import time

def bar():
time.sleep(3)
print("int the bar")
def test2(func):
print(func)
return func
print(test2(bar)) t=test2(bar)
bar=test2(bar)
t() #t()=bar()
print(t) bar=test2(bar) #覆盖原来的bar
bar() #不修改原来的调用方式 打印结果
——————————————————————————
<function bar at 0x00000224596500D0>
<function bar at 0x00000224596500D0>
<function bar at 0x00000224596500D0>
<function bar at 0x00000224596500D0>
int the bar
<function bar at 0x00000224596500D0>
<function bar at 0x00000224596500D0>
int the bar

最新文章

  1. 从SqlServer转手Oracle的一些坑
  2. Ubuntu开发笔记
  3. bugfree安装 centos
  4. .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  5. [DBW]js获取当前时间(昨天、今天、明天)
  6. 新建Oracle数据库时,提示使用database control配置数据库时,要求在当前oracle主目录中配置监听程序
  7. jQuery.lazyload详解
  8. iebugs产生的原因,zoom:1的作用
  9. Node基础:域名解析DNS(ok)
  10. mysql主从备份、主从切换的例子
  11. hdu 2481 Toy
  12. android service总结
  13. 有关于Algorithm的基础介绍
  14. 未来 USB Type-C 将可靠软体判断线材是否符合规定
  15. Dice (III) 概率dp
  16. git语言
  17. 武汉科技大学ACM :1006: A+B for Input-Output Practice (VI)
  18. linux mail 简操作
  19. Android studio 下的SDK Manager只显示已安装包的情况
  20. [js]浏览器同源策略(same-origin policy)

热门文章

  1. 点击其他区域关闭dialog
  2. JVM内存模型及配置参数
  3. jsp部分
  4. Linux 下 Mysql忘记密码重置
  5. Pytorch-创建tensor
  6. office web apps安装部署,配置https,负载均衡(六)配置负载均衡
  7. mysql数据库为什么要分表和分区?
  8. flask 之(六) --- API|RestfulApi
  9. MODFLOW几个版本的区别
  10. TensorFlow实战第六课(过拟合)