zoukankan      html  css  js  c++  java
  • Python装饰器

    Python装饰器

    之前对装饰器的理解就一句话:对一个函数,对其不做任何改变的条件下,增加新的功能。个人觉得,这句话并没有什么毛病,但是实际在代码中体现的时候,各种内函数,外函数的,理解起来还是比较晕,尤其是带参数的装饰器。。。

    • 先看普通装饰器

      1. 函数test1和函数test2是原始代码:

         def test1():
         	time.sleep(2)
         	print('in the T1')
        
         def test2(name,age):
         	time.sleep(1)
         	print('name is %s and age is %s'%(name,age))
        
         test1()
         test2('ZZl',25)
        
      2. 现在的需求是给这两个函数加一个功能,分别计算这两个函数执行的时间。假设函数test1和函数test2已经被其他地方调用,所以对其增加新的功能就不能改变他们的调用方式,也不能改变这两个函数的内部代码,所以这里用上了装饰器。。。

         import time
         #要增加的功能,将其定义为一个名为timer的函数
         def  timer(fun):
         	def deco(*args,**kwargs):
         		start_time = time.time()    	#开始时间
         		fun(*args,**kwargs)
         		stop_time = time.time()			#结束时间
         		print(stop_time - start_time)	#打印时间差
         	return deco
        
         @timer               #test1 = timer(test1)   覆盖之前的test1()
         def test1():
         	time.sleep(2)
         	print('in the T1')
         @timer               #test1 = timer(test1)   覆盖之前的test2()
         def test2(name,age):
         	time.sleep(1)
         	print('name is %s and age is %s'%(name,age))
        
        
         test1()
         test2('ZZl',25)
        
      3. 上面代码实现的原理就是将函数test1和函数test2作为参数传递给timer函数。

    • 带参数的装饰器

      1. 价格参数,将之前的@timer改成@timer(cs1),为一个cs1参数传递给timer()

         import time
         #要增加的功能,将其定义为一个名为timer的函数
         def  timer(canshu):
         	def deco(fun,*args,**kwargs):
         		start_time = time.time()    	#开始时间
         		fun(*args,**kwargs)
         		stop_time = time.time()			#结束时间
         		print(stop_time - start_time)	#打印时间差
         	return deco
        
         
         @timer(cs1)
         def test2(name,age):
         	time.sleep(1)
         	print('name is %s and age is %s'%(name,age))
        

        test2('ZZl',25)

      2. 解释器在执行代码的时候,在遇到@timer(cs1)这句时,做的操作是timer(cs1),开始跟语法糖 @ 没任何关系,先执行timer(cs1),也就跟调用timer(canshu)这个函数一样,只不过是实参是cs1,执行结果得到的是:deco(因为timer(canshu)函数return deco)。第二部就是@deco,@deco就跟上面的普通装饰器一样了,就是将test2作为参数传递给deco函数执行。。。

        解释器解释到这行: @timer(cs1)

        第一步执行: timer(cs1) ----->返回 deco

        第一步结果: @deco

        第二步执行: @deco

        第二步执行过程: deco(test2(name,age))

    3.有助于理解Flask的路由方法...

  • 相关阅读:
    宝宝打疫苗
    【小工具】2. 需要对测试用的数据进行MD5加密
    【小工具】1.需要对txt存放的测试数据做去重处理
    【Jenkins】定时构建语法
    【bug】记一个有趣的“bug”
    1.由于测试某个功能,需要生成500W条数据的txt,python代码如下
    开发基于vue前端框架下的系统的UI自动化,记录总结踩的坑
    使用Chrome-headless模式下,截屏不全屏的问题
    Chrome-headless 模式,没有UI界面的自动化UI测试
    【selenium】Webdriver的原理以及工作流程
  • 原文地址:https://www.cnblogs.com/MisterZZL/p/9672361.html
Copyright © 2011-2022 走看看