开放 ： 对扩展是开放的
封闭 ： 对修改是封闭的

# 不是装饰器的装饰器

import time

def func():
time.sleep(0.01)
print('123')

def timmer(f):
def inner():
start = time.time()
f()
end = time.time()
print(end-start)
return inner

func = timmer(func)	#执行装饰
func()

# 采用@的装饰器


import time

def timmer(f):
def inner():
start = time.time()
ret=f()
end = time.time()
print(end-start)
return ret
return inner

@timmer     #相当于执行了 func=timmer(func)
def func():
time.sleep(0.01)
print('123')
return 1

ret=func()
print(ret)

# 带输入输出的修饰器


import time

def timmer(f):
def inner(a):
start = time.time()
ret=f(a)
end = time.time()
print(end-start)
return ret
return inner

@timmer
def func(a):
time.sleep(0.01)
print('123')
a=a+1
return a

ret=func(2)
print(ret)

# 修饰器模板

def wrapper(func):   #func表示被修饰的函数
def inner(*args,**kwargs):
#函数前要执行的内容
ret = func(*args,**kwargs)   #被装饰的函数
#函数后要执行的内容
return ret
return inner

@wrapper        #执行修饰，相当于执行了qqxing = wrapper(qqxing)
def qqxing():
print(123)

ret = qqxing()

# 带开关的装饰器

import time
FLAGE = True
def timmer_out(flag):
def timmer(func):
def inner(*args,**kwargs):
if flag:
start = time.time()
ret = func(*args,**kwargs)
end = time.time()
print(end-start)
return ret
else:
ret = func(*args, **kwargs)
return ret
return inner
return timmer

@timmer_out(FLAGE)    #wahaha = timmer(wahaha)
def wahaha():
time.sleep(0.1)
print('wahahahahahaha')

wahaha()

# 多个装饰器装饰一个函数

#多个装饰器装饰一个函数
def wrapper1(func):
def inner1():
print('wrapper1 ,before func')
ret = func()
print('wrapper1 ,after func')
return ret
return inner1

def wrapper2(func):
def inner2():
print('wrapper2 ,before func')
ret = func()
print('wrapper2 ,after func')
return ret
return inner2

def wrapper3(func):
def inner3():
print('wrapper3 ,before func')
ret = func()
print('wrapper3 ,after func')
return ret
return inner3

@wrapper3
@wrapper2
@wrapper1
def f():
print('in f')
return '哈哈哈'

print(f())

wrapper3 ,before func
wrapper2 ,before func
wrapper1 ,before func
in f
wrapper1 ,after func
wrapper2 ,after func
wrapper3 ,after func