</>复制代码
上一篇文章:Python是动态语言:动态添加或删除属性、方法
下一篇文章:私有化规则与属性Property
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
我调用了
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/42245.html
摘要:以下这些项目,你拿来学习学习练练手。当你每个步骤都能做到很优秀的时候,你应该考虑如何组合这四个步骤,使你的爬虫达到效率最高,也就是所谓的爬虫策略问题,爬虫策略学习不是一朝一夕的事情,建议多看看一些比较优秀的爬虫的设计方案,比如说。 (一)如何学习Python 学习Python大致可以分为以下几个阶段: 1.刚上手的时候肯定是先过一遍Python最基本的知识,比如说:变量、数据结构、语法...
摘要:变量查找规则在中一个变量的查找顺序是局部环境,闭包,全局,内建闭包引用了自由变量的函数。闭包的作用闭包的最大特点是可以将父函数的变量与内部函数绑定,并返回绑定变量后的函数,此时即便生成闭包的环境父函数已经释放,闭包仍然存在。 导语:本文章记录了本人在学习Python基础之函数篇的重点知识及个人心得,打算入门Python的朋友们可以来一起学习并交流。 本文重点: 1、掌握装饰器的本质、功...
摘要:初步认识装饰器函数装饰器用于在源代码中标记函数,以某种方式增强函数的行为。函数装饰器在导入模块时立即执行,而被装饰的函数只在明确调用时运行。只有涉及嵌套函数时才有闭包问题。如果想保留函数原本的属性,可以使用标准库中的装饰器。 《流畅的Python》笔记本篇将从最简单的装饰器开始,逐渐深入到闭包的概念,然后实现参数化装饰器,最后介绍标准库中常用的装饰器。 1. 初步认识装饰器 函数装饰...
摘要:使用一年多了,一直知道有个装饰器,很好用,试图理解过,可能由于资料找的不好,自己的悟性太差,一直没有搞清楚,今天查了一些资料,算是理解了,现在简单记录下。 使用python一年多了,一直知道python有个装饰器,很好用,试图理解过,可能由于资料找的不好,自己的悟性太差,一直没有搞清楚,今天查了一些资料,算是理解了,现在简单记录下。python的装饰器本身的功能是在不改变已有函数本身的...
阅读 3271·2021-09-10 10:51
阅读 3460·2021-08-31 09:38
阅读 1780·2019-08-30 15:54
阅读 3205·2019-08-29 17:22
阅读 3298·2019-08-26 13:53
阅读 2044·2019-08-26 11:59
阅读 3354·2019-08-26 11:37
阅读 3388·2019-08-26 10:47