# _*_ coding: utf-8 _*_
# 装饰器的语法糖
# @装饰器的名字:要在被装饰对象正上方单独一行写上
import time
# 引用time内置函数
def timmer(func):
def wrapper(*args, **kwargs):
start = time.time()
res = func(*args, **kwargs)
stop = time.time()
print('Run time is %s' %(stop - start))
return res
return wrapper
#(new)index = timmer((旧)index的内存地址 = func)
# index的内存地址 = func
@timmer
def index():
print('Welcome to index page')
time.sleep(1)
@timmer#home = timmer(home)
def home(name):
print('welcome %s to home page' %name)
time.sleep(1)
return 888
index()
res = home('oOC')
print(res)
####无参装饰器模版####
def outter(func):
def wrapper(*args,**kwargs):
#在调用函数前加功能
res=func(*args,**kwargs) #调用被装饰的\也就是最原始的那个函数
#在调用函数后加功能
return res
return wrapper
# ////////////////////////////////////////////////////
@outter #index=outter(index) #index=wrapper
def index():
print('welcome to index page')
time.sleep(3)
index()