python 装饰器的使用示例
作者:南方的墙 时间:2021-05-15 00:52:09
无参修饰 ,无参数时不需要调用
def log1(func):
func()
@log1
def test():
print('test:')
有参修饰
def log2(func):
def inner(*args, **kwargs):
func(*args, **kwargs)
return inner
@log2
def test(num):
print('testlog2:',num,test.__name__)
test(20) #相当于log(test(20))
@wraps可以保证装饰器修饰的函数的name的值保持不变
不带参数的装饰器
def log3(func):
@wraps(func)
def inner(*args, **kwargs,):
func(*args, **kwargs)
return inner
@log3
def test(num):
print('testlog3:',num,test.__name__)
test(30) #相当于log(test(30))
带参数的装饰器
def log4(level):
def log(func):
@wraps(func)
def inner(*args, **kwargs,):
if level == "warn":
print("%s is running" % func.__name__)
func(*args, **kwargs)
return inner
return log
@log4(level="warn")
def test(num):
print('testlog4:', num, test.__name__)
test(40)
实现带参数和不带参数的装饰器自适应
def log(arg):
if callable(arg): # 判断参入的参数是否是函数,不带参数的装饰器调用这个分支
def log3(func):
@wraps(func)
def inner(*args, **kwargs, ):
func(*args, **kwargs)
return inner
return log3
else:
def log4(func):
@wraps(func)
def inner(*args, **kwargs,):
if arg == "warn":
print("%s is running" % func.__name__)
func(*args, **kwargs)
return inner
return log4
@log(arg=None)
def test(num):
print('testlog:', num, test.__name__)
test(0)
返回入参出参
def log5(func):
def inner(*args, **kwargs):
print('入参:',func.__name__, args, kwargs)
res =func(*args, **kwargs)
print('出参:',func.__name__, res)
return res
return inner
@log5
def test(num):
print('testlog5:', num, test.__name__)
return num
print(test(50))
类装饰器
class Loging:
def __init__(self,level):
self.level = level
def __call__(self,func):
@wraps(func)
def inner(*args, **kwargs):
if self.level == "warn":
self.notify(func)
func(*args, **kwargs)
return inner
def notify(self,func):
print ("%s is running" % func.__name__)
@Loging(level="warn")
def test(num):
print('testLoging:', num, test.__name__)
test(0)
来源:https://www.cnblogs.com/shuzf/p/13434477.html
标签:python,装饰器,示例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ASP-server.URLEncode反函数:urldecode
2008-10-23 16:05:00
django 将model转换为字典的方法示例
2022-09-16 14:03:09
python Plotly绘图工具的简单使用
2023-06-13 01:16:17
![](https://img.aspxhome.com/file/2023/8/86648_0s.png)
经验总结:ASP与存储过程解析
2008-11-07 13:33:00
简单上手Python中装饰器的使用
2023-06-20 19:20:06
opencv 图像滤波(均值,方框,高斯,中值)
2022-12-04 23:41:07
![](https://img.aspxhome.com/file/2023/9/82529_0s.jpg)
js友好的表单验证程序vform
2007-08-16 13:32:00
为什么JavaScript中正则表达式的test方法会出错?
2009-03-17 12:38:00
OpenCV图像处理GUI功能详解
2021-01-26 15:55:34
![](https://img.aspxhome.com/file/2023/4/76754_0s.png)
windowns使用PySpark环境配置和基本操作
2021-04-12 06:43:06
![](https://img.aspxhome.com/file/2023/3/95123_0s.png)
python2 对excel表格操作完整示例
2022-08-27 00:35:06
![](https://img.aspxhome.com/file/2023/6/85156_0s.png)
python和php哪个更适合写爬虫
2023-10-28 00:51:14
django_orm查询性能优化方法
2021-03-01 20:30:19
Django 2.0版本的新特性抢先看!
2021-01-03 11:10:50
Django实现文件上传下载
2022-03-17 18:42:48
Python循环语句介绍
2021-04-19 20:04:42
![](https://img.aspxhome.com/file/2023/9/103219_0s.png)
python中设置超时跳过,超时退出的方式
2023-12-25 01:38:20
Python编程使用matplotlib绘制动态圆锥曲线示例
2021-08-30 03:38:18
![](https://img.aspxhome.com/file/2023/6/85096_0s.png)
PHP程序员玩转Linux系列 使用supervisor实现守护进程
2023-10-12 14:50:21
![](https://img.aspxhome.com/file/2023/4/65814_0s.jpg)
python实现求纯色彩图像的边框
2022-04-01 22:04:51
![](https://img.aspxhome.com/file/2023/4/109974_0s.jpg)