python装璜器详解
python装璜器的具体解析
甚么是装璜器?
保举学习:Python视频教程
python装璜器(fuctional decorators)就是用于拓展原来函数性能的一种函数,目的是正在没有扭转原函数名(或类名)的状况下,给函数添加新的性能。
这个函数的非凡的地方正在于它的前往值也是一个函数,这个函数是内嵌“原“”函数的函数。
普通而言,咱们要想拓展原来函数代码,最间接的方法就是侵入代码外面修正,例如:
import time def f(): print("hello") time.sleep(1) print("world")
这是咱们最原始的的一个函数,而后咱们试图记载下这个函数执行的总工夫,那最简略的做法就是改动原来的代码:
import time def f(): start_time = time.time() print("hello") time.sleep(1) print("world") end_time = time.time() execution_time = (end_time - start_time)*1000 print("time is %d ms" %execution_time)
然而实际工作中,有些时分外围代码其实不能够间接去改,以是正在没有改动原代码的状况下,咱们能够再界说一个函数。(然而失效需求再次执行函数)
import time def deco(func): start_time = time.time() f() end_time = time.time() execution_time = (end_time - start_time)*1000 print("time is %d ms" %execution_time) def f(): print("hello") time.sleep(1) print("world") if __name__ == '__main__': deco(f) print("f.__name__ is",f.__name__) print()
这里咱们界说了一个函数deco,它的参数是一个函数,而后给这个函数嵌入了计时性能。然而想要拓展这一万万个函数性能,
就是要执行一万万次deco()函数,以是这样其实不理想!接上去,咱们能够试着用装璜器来完成,先看看装璜器最原始的风貌。
import time def deco(f): def wrapper(): start_time = time.time() f() end_time = time.time() execution_time = (end_time - start_time)*1000 print("time is %d ms" %execution_time ) return wrapper @deco def f(): print("hello") time.sleep(1) print("world") if __name__ == '__main__': f()
这里的deco函数就是最原始的装璜器,它的参数是一个函数,而后前往值也是一个函数。
此中作为参数的这个函数f()就正在前往函数wrapper()的外部执行。而后正在函数f()后面加之@deco,
f()函数就相称于被注入了计时性能,如今只需挪用f(),它就曾经变身为“新的性能更多”的函数了,
(没有需求反复执行原函数)。
扩大1:带有固定参数的装璜器
import time def deco(f): def wrapper(a,b): start_time = time.time() f(a,b) end_time = time.time() execution_time = (end_time - start_time)*1000 print("time is %d ms" % execution_time) return wrapper @deco def f(a,b): print("be on") time.sleep(1) print("result is %d" %(a+b)) if __name__ == '__main__': f(3,4)
扩大2:无固定参数的装璜器
import time def deco(f): def wrapper(*args, **kwargs): start_time = time.time() f(*args, **kwargs) end_time = time.time() execution_time_ = (end_time - start_time)*1000 print("time is %d ms" %execution_time) return wrapper @deco def f(a,b): print("be on") time.sleep(1) print("result is %d" %(a+b)) @deco def f2(a,b,c): print("be on") time.sleep(1) print("result is %d" %(a+b+c)) if __name__ == '__main__': f2(3,4,5) f(3,4)
扩大3:应用多个装璜器,装璜一个函数
import time def deco01(f): def wrapper(*args, **kwargs): print("this is deco01") start_time = time.time() f(*args, **kwargs) end_time = time.time() execution_time = (end_time - start_time)*1000 print("time is %d ms" % execution_time) print("deco01 end here") return wrapper def deco02(f): def wrapper(*args, **kwargs): print("this is deco02") f(*args, **kwargs) print("deco02 end here") return wrapper @deco01 @deco02 def f(a,b): print("be on") time.sleep(1) print("result is %d" %(a+b)) if __name__ == '__main__': f(3,4)
''' this is deco01 this is deco02 hello,here is a func for add : result is 7 deco02 end here time is 1003 ms deco01 end here '''
装璜器挪用程序
装璜器是能够叠加应用的,那末应用装璜器当前代码是啥程序呢?
关于Python中的”@”语法糖,装璜器的挪用程序与应用 @ 语法糖申明的程序相同。
正在这个例子中,”f(3, 4) = deco01(deco02(f(3, 4)))”。
Python内置装璜器
正在Python中有三个内置的装璜器,都是跟class相干的:staticmethod、classmethod 以及property。
staticmethod 是类动态办法,其跟成员办法的区分是不 self 参数,而且能够正在类没有进行实例化的状况下挪用
classmethod 与成员办法的区分正在于所接纳的第一个参数没有是 self (类实例的指针),而是cls(以后类的详细类型)
property 是属性的意义,示意能够经过经过类实例间接拜访的信息
关于staticmethod以及classmethod这里就没有引见了,经过一个例子看看property。
留意,关于Python旧式类(new-style class),假如将下面的 “@var.setter” 装璜器所装璜的成员函数去掉,则Foo.var 属性为只读属性,应用 “foo.var = ‘var 2′” 进行赋值时会抛出异样。然而,关于Python classic class,所申明的属性没有是 read-only的,以是即便去掉”@var.setter”装璜器也没有会报错。
总结
本文引见了Python装璜器的一些应用,装璜器的代码仍是比拟容易了解的。只需经过一些例子进行实际操作一下,就很容易了解了。
以上就是python 装璜器详解的具体内容,更多请存眷资源魔其它相干文章!
标签: Python python教程 python编程 python使用问题
抱歉,评论功能暂时关闭!