Python装饰器原理与基本用法分析
作者:随风行云 时间:2023-04-29 10:46:32
本文实例讲述了Python装饰器原理与基本用法。分享给大家供大家参考,具体如下:
装饰器:
意义:在不能改变原函数的源代码,和在不改变整个项目中原函数的调用方式的情况下,给函数添加新的功能
由于不允许改变函数的源代码,在忽略调用方式的情况下,我们可能会有以下结果:
def decorator(func):
func()
print("logging")
def test1():
print("test1")
def test2():
print("Test2")
decorator(test1)
decorator(test2)
但这改变了原本的调用方式,原本是test1(),现在是decorator(test1)
那么如果我们为了使调用方式不变,是否可以使装饰好的函数decorator的返回值是一个我们需要的函数,再赋值给原来的函数名呢?
于是:
def timmer1(func):
def warpper():
start_time = time.time()
func()
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper
test3=timmer1(test3)
好像上面这段代码并没有改变原来的调用方式,调用原来的test3,相当于运行timmer1中的warpper
如果对于无参数的函数来说,上面的代码已经实现了我们的目的,但对于带参数的函数,上面的代码没有传入参数,所以仍然需要修改
于是:
def timmer2(func):
def warpper(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper
在上上面的代码中,由于实质上,test3已经等于wrapper,所以可以直接使用,test3(参数)来传入参数,为了处理参数不确定数量问题,可以使用可变长度参数
上面代码还存在一个问题,无法获取原本函数中的返回值,那么我们还需要加上一些东西:
import time
def timmer2(func):
def warpper(*args,**kwargs):
start_time = time.time()
res=func(*args,**kwargs)
return res
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper
使用一个变量记录下原函数的返回值。
这样我们就实现了装饰器的基本功能。
补充:
python提供了一个功能:
@装饰器名
def 目标装饰函数名():
pass
#上面的效果是 目标装饰函数名=装饰器(目标装饰函数名)
所以在需要替换原函数的时候,可以在目标装饰函数定义的上一行加上@装饰器名
所以上面的代码会变成:
def timmer2(func):
def warpper(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper
@timmer2
def test7():
print("test7")
@timmer2
def test6(x):
print(x)
test7()
test6(2)
import time
def timmer2(func):
def warpper(*args,**kwargs):
start_time = time.time()
res=func(*args,**kwargs)
return res
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time))
return warpper
@timmer2
def test4():
print("test4 run")
return "test4 done"
test4()
print("--------")
print(test4())
第二个补充:
可以一个函数,可以使用多个装饰器
比如:
@装饰器1
@装饰器2
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/progor/p/8410776.html
标签:Python,装饰器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何基于python3和Vue实现AES数据加密
2023-08-08 23:47:37
Python检测和防御DOS攻击的最简单方法
2022-04-12 17:23:19
![](https://img.aspxhome.com/file/2023/1/79551_0s.jpg)
详解MySQL高可用MMM搭建方案及架构原理
2024-01-18 07:28:09
![](https://img.aspxhome.com/file/2023/4/129904_0s.png)
Python+Pygame制作"长沙版"大富翁
2023-10-05 06:53:08
![](https://img.aspxhome.com/file/2023/8/65088_0s.png)
对内联文字的疑惑
2008-04-18 12:19:00
MySQL 集群配置
2009-04-20 14:15:00
url传递中文的解决方案
2007-10-09 20:17:00
MySQL时间盲注的五种延时方法实现
2024-01-21 19:46:16
![](https://img.aspxhome.com/file/2023/1/121581_0s.png)
vue+elementUi图片上传组件使用详解
2024-05-10 14:14:49
Python tkinter事件高级用法实例
2023-12-09 10:28:17
![](https://img.aspxhome.com/file/2023/5/93685_0s.jpg)
原生js封装的一些jquery方法(详解)
2024-04-19 09:47:01
tensorboard实现同时显示训练曲线和测试曲线
2023-05-18 04:55:34
![](https://img.aspxhome.com/file/2023/1/65541_0s.jpg)
浅谈django三种缓存模式的使用及注意点
2022-08-24 20:22:25
![](https://img.aspxhome.com/file/2023/2/75142_0s.png)
教你用scrapy框架爬取豆瓣读书Top250的书类信息
2022-11-04 12:48:23
![](https://img.aspxhome.com/file/2023/8/118568_0s.jpg)
Python通用验证码识别OCR库之ddddocr验证码识别
2021-05-16 22:55:00
![](https://img.aspxhome.com/file/2023/4/126914_0s.jpg)
python获取对象信息的实例详解
2022-04-30 14:55:50
深入理解Python爬虫代理池服务
2022-11-05 17:20:29
![](https://img.aspxhome.com/file/2023/9/100819_0s.png)
详解vue-router和vue-cli以及组件之间的传值
2024-05-21 10:31:10
![](https://img.aspxhome.com/file/2023/1/123921_0s.jpg)
python matplotlib 绘图 和 dpi对应关系详解
2023-12-11 18:13:43
![](https://img.aspxhome.com/file/2023/7/99477_0s.jpg)
php实现的三个常用加密解密功能函数示例
2023-07-20 06:25:52
![](https://img.aspxhome.com/file/2023/4/55424_0s.jpg)