python装饰器详解

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time : 2018/9/27 9:33
# @Author : 王巍
# @Site :
# @File : 装饰器.py
# @Software: PyCharm


#装饰器:
#简言之,python装饰器就是用于拓展原来函数功能的一种函数,这个函数的特殊之处在于它的返回值也是一个函数,使用python装饰器的好处就是在不用更改原函数的代码前提下给函数增加新的功能。


#1原始函数
# import time
# def func():
# print("hello")
# time.sleep(1)
# print("world")
# print(func())

#2原始侵入,篡改原函数:实现记录下这个函数执行的总时间
# import time
# def func():
# start_time=time.time()
# print("hello")
# time.sleep(1)
# print("world")
# end_time=time.time()
# mescs=(end_time-start_time)*1000
# print("time is %d ms"%(mescs))


#3这段代码是我们公司的核心代码,你不能直接去改我们的核心代码。
#避免直接侵入原函数修改,但是生效需要再次执行函数

# import time
#
# def deco(func):
# start_time=time.time()
# func()
# end_time=time.time()
# mesc=(end_time-start_time)*1000
# print("time is %d ms"%(mesc))
#
# def test():
# print("hello")
# time.sleep(1)
# print("world")
#
# if __name__=="__main__":
# test=deco(test)#只有把func()或者f()作为参数执行,新加入功能才会生效
# print(test)
#


#4既不需要侵入,也不需要函数重复执行
#解释:这里的deco函数就是最原始的装饰器,它的参数是一个函数,然后返回值也是一个函数。其中作为参数的这个函数func()就在返回函数wrapper()的内部执行。
# 然后在函数func()前面加上@deco,func()函数就相当于被注入了计时功能,现在只要调用func(),它就已经变身为“新的功能更多”的函数了。
#所以这里装饰器就像一个注入符号:有了它,拓展了原来函数的功能既不需要侵入函数内更改代码,也不需要重复执行原函数。

# import time
# def deco(func): #装饰器:它的参数是一个函数,然后返回值也是一个函数
# def wrapper():
# start_time=time.time()
# func()
# end_time=time.time()
# mescs=(end_time-start_time)*1000
# print("time is %d ms"%(mescs))
# return wrapper
#
# @deco
# def test():
# print("hello")
# time.sleep(1)
# print("world")
#
# if __name__=='__main__':
# f=test#这里f被赋值为test,执行f()就是执行test()
# f()
#test()





#5带有参数的装饰器
# import time
# def deco(func):
# def wrapper(a,b):
# start_time=time.time()
# func(a,b)
# end_time=time.time()
# mescs=(end_time-start_time)*1000
# print("time is %d ms"%(mescs))
# return wrapper
#
# @deco
# def test(a,b):
# print("hello,here is a func for add:")
# time.sleep(1)
# print("result is %d"%(a+b))
#
# if __name__=="__main__":
# f=test
# f(3,5)




#6带有不定参数的装饰器
# import time
# def deco(func):
# def wrapper(*args,**kwargs): #*args是非关键字参数,用于元组,**kw是关键字参数,用于字典
# start_time=time.time()
# func(*args,**kwargs)
# end_time=time.time()
# mesce=(end_time-start_time)*1000
# print("time is %d ms"%(mesce))
# return wrapper
#
# @deco
# def test1(a,b):
# print("hello,here is func for add1:")
# time.sleep(1)
# print("result is %d"%(a+b))
#
# @deco
# def test2(a,b,c):
# print("hello,here is a func for add2:")
# time.sleep(1)
# print("result is %d"%(a+b+c))
#
# if __name__=="__main__":
# test1(3,6)
# test2(1,122,1334)




#7多个装饰器
import time
def deco1(func):
def wrapper(*args,**kwargs):
print("this is deco1")
start_time=time.time()
func(*args,**kwargs)
end_time=time.time()
mesce=(end_time-start_time)*1000
print("time is %d ms"%(mesce))
print("deco1 end here")
return wrapper

def deco2(func):
def wrapper(*args,**kwargs):
print("this is deco2")
start_time=time.time()
func(*args,**kwargs)
end_time=time.time()
mesce=(end_time-start_time)*1000
print("time is %d ms"%(mesce))
print("deco2 end here")
return wrapper

#8多个装饰器执行的顺序就是从最后一个装饰器开始,执行到第一个装饰器,再执行函数本身
@deco1
@deco2
def test(a,b):
print("hello,here is a func for add:")
time.sleep(1)
print("result is %d"%(a+b))

if __name__ == '__main__':
f=test
f(3,9)
posted @ 2018-09-27 14:01  巍然~chelsea  阅读(180)  评论(0编辑  收藏  举报