Python3.5裝飾器典型案例分析
本文實(shí)例講述了Python3.5裝飾器。分享給大家供大家參考,具體如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:ZhengzhengLiu #高階函數(shù)+嵌套函數(shù)==>裝飾器 import time def timer(func): #timer(test1)-->func=test1 def decor(): start_time = time.time() func() #run test1 stop_time = time.time() print("the run time of func is %s" %(stop_time-start_time)) return decor @timer #test1 = timer(test1) def test1(): time.sleep(3) print("in the test1") @timer #test2 = timer(test2) def test2(): time.sleep(3) print("in the test2") print(timer(test1)) #打印deco的地址 #test1 = timer(test1) #test2 = timer(test2) test1() #-->執(zhí)行decor test2()
運(yùn)行結(jié)果:
<function timer.<locals>.decor at 0x00B720C0>
in the test1
the run time of func is 3.000171661376953
in the test2
the run time of func is 3.000171661376953
1、裝飾器修飾有參數(shù)函數(shù)
#高階函數(shù)+嵌套函數(shù)==>裝飾器 import time def timer(func): #timer(test1)-->func=test1 def decor(arg1,arg2): start_time = time.time() func(arg1,arg2) #run test2 stop_time = time.time() print("the run time of func is %s" %(stop_time-start_time)) return decor @timer #test2 = timer(test2) = decor test2(name)==>decor(name) def test2(name,age): print("test2:",name,age) test2("liu",23)
運(yùn)行結(jié)果 :
test2: liu 23
the run time of func is 0.0
2、裝飾器修飾多個(gè)函數(shù),有的函數(shù)帶參數(shù),有的函數(shù)不帶參數(shù)的情況(采用參數(shù)組)
#高階函數(shù)+嵌套函數(shù)==>裝飾器 import time def timer(func): #timer(test1)-->func=test1 def decor(*args,**kwargs): start_time = time.time() func(*args,**kwargs) #run test1 stop_time = time.time() print("the run time of func is %s" %(stop_time-start_time)) return decor @timer #test1 = timer(test1) def test1(): time.sleep(3) print("in the test1") @timer #test2 = timer(test2) = decor test2(name)==>decor(name) def test2(name,age): time.sleep(1) print("test2:",name,age) #test1 = timer(test1) #test2 = timer(test2) test1() #-->執(zhí)行decor test2("liu",23)
運(yùn)行結(jié)果:
in the test1
the run time of func is 3.0036065578460693
test2: liu 23
the run time of func is 1.0084023475646973
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門(mén)與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- 實(shí)例講解Python編程中@property裝飾器的用法
- Python黑魔法@property裝飾器的使用技巧解析
- 介紹Python的@property裝飾器的用法
- 淺談解除裝飾器作用(python3新增)
- Python3.7 新特性之dataclass裝飾器
- python3 property裝飾器實(shí)現(xiàn)原理與用法示例
- Python3.5裝飾器原理及應(yīng)用實(shí)例詳解
- python類裝飾器用法實(shí)例
- Python 使用類寫(xiě)裝飾器的小技巧
- Python類裝飾器實(shí)現(xiàn)方法詳解
- python3.6中@property裝飾器的使用方法示例
相關(guān)文章
使用C語(yǔ)言來(lái)擴(kuò)展Python程序和Zope服務(wù)器的教程
這篇文章主要介紹了使用C語(yǔ)言來(lái)擴(kuò)展Python程序和Zope服務(wù)器的教程,本文來(lái)自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)及numpy統(tǒng)計(jì)函數(shù)
這篇文章主要介紹了Python實(shí)現(xiàn)Mysql數(shù)據(jù)統(tǒng)計(jì)的實(shí)例代碼,給大家介紹了Python數(shù)據(jù)分析numpy統(tǒng)計(jì)函數(shù)的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07Python Pandas知識(shí)點(diǎn)之缺失值處理詳解
這篇文章主要給大家介紹了關(guān)于Pandas知識(shí)點(diǎn)之缺失值處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05解析Mac OS下部署Pyhton的Django框架項(xiàng)目的過(guò)程
這篇文章主要介紹了Mac OS下部署Pyhton的Django框架項(xiàng)目的過(guò)程,還附帶將了一個(gè)gunicorn結(jié)合Nginx來(lái)部署Django應(yīng)用的方法,需要的朋友可以參考下2016-05-05Python設(shè)置在shell腳本中自動(dòng)補(bǔ)全功能的方法
今天小編就為大家分享一篇Python設(shè)置在shell腳本中自動(dòng)補(bǔ)全功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06python3實(shí)現(xiàn)的zip格式壓縮文件夾操作示例
這篇文章主要介紹了python3實(shí)現(xiàn)的zip格式壓縮文件夾操作,結(jié)合實(shí)例形式分析了Python3基于zipfile模塊實(shí)現(xiàn)zip格式文件壓縮的相關(guān)操作技巧,需要的朋友可以參考下2019-08-08