装饰器就是闭包函数的一种应用场景

一、为何要用装饰器

开放封闭原则:软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的
       也就是说我们必须找到一种解决方案:能够在不修该一个功能源代码以及调用方式的前提下,为其加上新功能
       上述总结,原则如下:
                1、不修改源代码
                2、不修改调用方式
        目的:
            在遵循1和2原则的基础上扩展新功能

二、什么是装饰器

器指的工具,装饰指的是为被装饰对象添加新功能

完整含义:
        装饰器即在不修改被装饰对象源代码与调用方式的前提下,为被装饰器对象添加新功能

装饰器与被装饰的对象均可以是任意可调用的对象

装饰器=》函数

被装饰的对象=》函数

三、无参装饰器

#无参装饰器模板
def outer(func):
def inner(*args,**kwargs):
res=func(*args,**kwargs)
return res
return inner
 ###现在我们要查看指令执行的时间
#以前实现的方式
#违反了装饰器的原则---》不修改源代码
import time #导入时间模块 def index():
start_time=time.time()       #现在时间
time.sleep(3)              #暂停3s
print('welcome to index page')    
stop_time=time.time()          #现在的时间(time.sleep与print执行完后的时间)
print('run time is %s' %(stop_time-start_time)) index() #调用函数 #修订1
#没有违背原则,但是我又要给一个程序加一个同样的这样的功能呢?会体现出现在的代码重复
import time def index():
time.sleep(3)
print('welcome to index page') start_time=time.time()
index()
stop_time = time.time()
print('run time is %s' % (stop_time - start_time)) #但是我又要给一个程序加一个同样的这样的功能呢?会体现出现在的代码重复 import time def index():
time.sleep(3)
print('welcome to index page') def home(name):
time.sleep(5)
print('welcome %s to home page' %name) start_time=time.time() #代码重复
index()
stop_time = time.time()      #代码重复
print('run time is %s' % (stop_time - start_time)) #代码重复 start_time=time.time()
home('egon')
stop_time = time.time()
print('run time is %s' % (stop_time - start_time)) #修订2 # 修改了原函数的调用方式 import time def index():
time.sleep(3)
print('welcome to index page') def home(name):
time.sleep(5)
print('welcome %s to home page' %name) def wrapper(func): #func=index
start_time=time.time()
func() #index()
stop_time = time.time()
print('run time is %s' % (stop_time - start_time)) wrapper(index) # 修改了原函数的调用方式 #修订3 import time def index():
time.sleep(3)
print('welcome to index page') def outter(func): #func=最原始的index
# func=最原始的index
def wrapper():
start_time=time.time()
func()
stop_time=time.time()
print(stop_time-start_time)
return wrapper index=outter(index) # 新的index=wrapper index() #wrapper()

无参装饰器

四、无参装饰器升级版

import time

def index():
time.sleep(1)
print('welcome to index page')
return 122 def home(name):
time.sleep(2)
print('welcome %s to home page' %name) #==============装饰器
def timmer(func):
#func=最原始的home
def wrapper(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs) #调用最原始的home
stop_time=time.time()
print(stop_time-start_time)
return res
return wrapper index=timmer(index) # 新的index=wrapper
home=timmer(home) #新的home=wrapper
# ========================================== # res=index() #res=wrapper()
# print(res) home(name='egon') #wrapper('egon')
index() #wrapper()

五、装饰器语法

#就是一个@调用某个装饰器

import time
def timmer(func):
def wrapper(*args,**kwargs):
start_time=time.time()
res=func(*args,**kwargs)
stop_time=time.time()
print(stop_time-start_time)
return res
return wrapper @timmer #index=timmer(index) #语法调用方式
def index():
time.sleep(1)
print('welcome to index page')
return 122 @timmer # home=timmer(home) #语法调用方式
def home(name):
time.sleep(2)
print('welcome %s to home page' %name) # index()
home('egon')

六、认证装饰器实现

七、叠加多个装饰器

八、有参数装饰器

最新文章

  1. centos7.0 下安装git(ssh方式)
  2. pyqt的信号槽机制(转)
  3. 一个assert的写法
  4. 原创:C语言打开、下载、删除网页,统计网页字符个数
  5. wcf服务返回json
  6. 随机数产生random
  7. hdu 5718 Oracle 高精度
  8. C语言——递归练习
  9. mysql:学习学习~
  10. [itint5]字符串匹配
  11. oracle中drop、delete和truncate的区别
  12. 大型分布式C++框架《三:序列化与反序列化》
  13. iOS面试题05-父子控制器、内存管理
  14. PHP部分--文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤
  15. Jenkins : 邮件通知
  16. Nginx集群之SSL证书的WebApi令牌验证
  17. 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)
  18. LeetCode算法题-Can Place Flowers(Java实现)
  19. 2D过渡模块的其他属性
  20. Linux - 日志处理一

热门文章

  1. eclipse集成testng插件(离线安装方式)
  2. JY播放器【蜻蜓FM电脑端,附带下载功能】
  3. Java飞机大战MVC版
  4. Ubuntu—安装python的第三方包gevent
  5. python socket详解
  6. Factorials 阶乘(思维)
  7. HDU 5203 Rikka with wood sticks 分类讨论
  8. 关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
  9. 防御 XSS 的七条原则
  10. SQL Server 一些操作语句