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