装饰器:不修改源码的前提下为函数增加功能
# 函数式不带参数装饰器
def Closepkg(func):
def wrapper(*args,**kwargs):
print(time.time())
# 添加打印时间
func(*args,**kwargs)
return wrapper
# 函数式,带参数装饰器
def zhuang(value):
def zhuang1(func):
def wrapper(*args,**kwargs):
print(value)
func(*args,**kwargs)
return wrapper
return zhuang1
#类式不带参数装饰器
class zhuang(object):
def __init__(self,func):
self.func = func
def __call__(self,*args,**kwargs):
print(time.time())
return self.func(*args,**kwargs)
# 类式带参数装饰器
class Zhuang(object):
def __init__(self,value):
self.value=value
def __call__(self,func):
def wrapper(*args,**kwargs)
print(value)
func(*args,**kwargs)
return wrapper
ps:有错误之处,望不吝指教