赵走x博客
网站访问量:151920
首页
书籍
软件
工具
古诗词
搜索
登录
24、nonlocal
23、Python functools.wraps 深入理解
21、Image.rotate旋转90度图片被截取了
20、python的__get__方法看这一篇就足够了
19、结巴分词(自然语言处理之中文分词器)
18、virtualenv
17、Python string 去掉标点符号 最佳实践
16、python 字符串相似度
15、StringIO和BytesIO
14、基于Python __dict__与dir()的区别详解
13、 set()去重的底层原理
12、顺序表的原理与python中的list类型
11、psutil实现系统监控
10、NSQ
9、utf-8的中文是一个汉字占三个字节长度吗?
8、supervisor+gunicorn部署python web项目
7、socket编程
6、async
5、Python的共享经济
4、Python 内存分配时的小秘密
3、Python中的“特权种族”是什么?
2、装饰器
1、序列化与反序列化
2、装饰器
资源编号:75760
Python
Python 查缺补漏
热度:89
> 装饰器功能: > > 1. 引入日志 > 2. 函数执行时间统计 > 3. 执行函数前预备处理 > 4. 执行函数后清理功能 > 5. 权限校验 > 6. 缓存
> 装饰器功能: > > 1. 引入日志 > 2. 函数执行时间统计 > 3. 执行函数前预备处理 > 4. 执行函数后清理功能 > 5. 权限校验 > 6. 缓存 ### 1、无参数函数的装饰器 实例: ``` from time import ctime,sleep def time_fun(func): #内部包裹函数 def wrapped_fun(): #ctime():打印当前时间 print("%s 在 %s 时被调用"%(func.__name__,ctime())) #执行函数 func() #把内部嵌套函数作为对象返回 return wrapped_fun @time_fun def test(): print("test 执行了") test() #休眠3秒 sleep(3) test() ``` 结果: ``` test 在 Wed Aug 15 22:19:51 2018 时被调用 test 执行了 test 在 Wed Aug 15 22:19:53 2018 时被调用 test 执行了 ``` ### 2、有参数函数的装饰器 实例: ``` from time import ctime,sleep def time_fun(func): #内部包裹函数 def wrapped_fun(a,b): #ctime():打印当前时间 print("%s 在 %s 时被调用"%(func.__name__,ctime())) #执行函数执行 func(a,b) #把内部嵌套函数作为对象返回 return wrapped_fun @time_fun def test(a,b): print(a+b) test(1,2) #休眠3秒 sleep(3) test(3,4) ``` 结果: ``` test 在 Wed Aug 15 22:23:07 2018 时被调用 3 test 在 Wed Aug 15 22:23:10 2018 时被调用 7 ``` ### 3、不定长函数的装饰器 实例: ``` from time import ctime,sleep def time_fun(func): #内部包裹函数 def wrapped_fun(*args,**kwargs): #ctime():打印当前时间 print("%s 在 %s 时被调用"%(func.__name__,ctime())) #执行函数执行 func(*args,**kwargs) #把内部嵌套函数作为对象返回 return wrapped_fun @time_fun def test(a,b,c): print(a+b+c) test(1,2,3) #休眠3秒 sleep(3) test(3,4,5) ``` 结果: ``` test 在 Wed Aug 15 22:26:36 2018 时被调用 6 test 在 Wed Aug 15 22:26:39 2018 时被调用 12 ``` ###4、带返回值函数的装饰器 实例: ``` from time import ctime,sleep def time_fun(func): #内部包裹函数 def wrapped_fun(*args,**kwargs): #ctime():打印当前时间 print("%s 在 %s 时被调用"%(func.__name__,ctime())) #执行函数执行 return func(*args,**kwargs) #把内部嵌套函数作为对象返回 return wrapped_fun @time_fun def test(a,b,c): print("test--",a+b+c) @time_fun def test2(a,b,c): return a+b+c test(1,2,3) print(test2(1,2,3)) #休眠3秒 sleep(3) test(1,2,3) print(test2(3,4,5)) ``` 结果: ``` test 在 Wed Aug 15 22:31:14 2018 时被调用 test-- 6 test2 在 Wed Aug 15 22:31:14 2018 时被调用 6 test 在 Wed Aug 15 22:31:17 2018 时被调用 test-- 6 test2 在 Wed Aug 15 22:31:17 2018 时被调用 12 ``` ### 5、装饰器带有参数 实例: ``` from time import ctime,sleep def time_fun_pre(pre="hello"): def time_fun(func): # 内部包裹函数 def wrapped_fun(*args, **kwargs): # ctime():打印当前时间 print("%s 在 %s 时被调用,pre参数为:%s" % (func.__name__, ctime(),pre)) # 执行函数执行 return func(*args, **kwargs) # 把内部嵌套函数作为对象返回 return wrapped_fun return time_fun @time_fun_pre("mark_test") def test(a,b,c): print("test--",a+b+c) @time_fun_pre("mark_test2") def test2(a,b,c): return a+b+c test(1,2,3) print(test2(1,2,3)) #休眠3秒 sleep(3) test(1,2,3) print(test2(3,4,5)) ``` 结果: ``` test 在 Wed Aug 15 22:43:27 2018 时被调用,pre参数为:mark_test test-- 6 test2 在 Wed Aug 15 22:43:27 2018 时被调用,pre参数为:mark_test2 6 test 在 Wed Aug 15 22:43:30 2018 时被调用,pre参数为:mark_test test-- 6 test2 在 Wed Aug 15 22:43:30 2018 时被调用,pre参数为:mark_test2 12 ``` ### 6、类装饰器 > python类装饰性必须要接受一个callable对象作为参数,然后返回一个callable对象,在python中,一般callable对象都是函数, > > 只要对象重写了`__call__()方法,那么这个对象就是callable对象。` 实例: ``` class Test(): def __init__(self,func): print("test初始化:",func.__name__) self.func=func def __call__(self, *args, **kwargs): print("我调用了") self.func @Test def test(): print("--test--") test() ``` 结果: ``` test初始化: test 我调用了 ```