最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3.5裝飾器典型案例分析

 更新時(shí)間:2019年04月30日 10:00:24   作者:loveliuzz  
這篇文章主要介紹了Python3.5裝飾器,結(jié)合實(shí)例形式分析了裝飾器修飾有參數(shù)函數(shù)、裝飾器修飾函數(shù)參數(shù)等情況相關(guān)使用技巧,需要的朋友可以參考下

本文實(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入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • python pandas模塊進(jìn)行數(shù)據(jù)分析

    python pandas模塊進(jìn)行數(shù)據(jù)分析

    Python的Pandas模塊是一個(gè)強(qiáng)大的數(shù)據(jù)處理工具,可以用來(lái)讀取、處理和分析各種數(shù)據(jù),本文主要介紹了python pandas模塊進(jìn)行數(shù)據(jù)分析,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-01-01
  • python的構(gòu)建工具setup.py的方法使用示例

    python的構(gòu)建工具setup.py的方法使用示例

    本篇文章主要介紹了python的構(gòu)建工具setup.py的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • Python檢查ping終端的方法

    Python檢查ping終端的方法

    今天小編就為大家分享一篇Python檢查ping終端的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 安裝不同版本的tensorflow與models方法實(shí)現(xiàn)

    安裝不同版本的tensorflow與models方法實(shí)現(xiàn)

    這篇文章主要介紹了安裝不同版本的tensorflow與models方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python開(kāi)發(fā)工具PyCharm的下載與安裝步驟圖文教程

    Python開(kāi)發(fā)工具PyCharm的下載與安裝步驟圖文教程

    這篇文章主要為大家介紹了Python開(kāi)發(fā)工具PyCharm的下載與安裝步驟圖文教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Python中的線程操作模塊(oncurrent)

    Python中的線程操作模塊(oncurrent)

    這篇文章介紹了Python中的線程操作模塊(oncurrent),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • Python內(nèi)置的字符串處理函數(shù)整理

    Python內(nèi)置的字符串處理函數(shù)整理

    Python內(nèi)置的字符串處理函數(shù)整理,收集常用的Python 內(nèi)置的各種字符串處理 函數(shù)的使用方法
    2013-01-01
  • Python時(shí)間差中seconds和total_seconds的區(qū)別詳解

    Python時(shí)間差中seconds和total_seconds的區(qū)別詳解

    今天小編就為大家分享一篇Python時(shí)間差中seconds和total_seconds的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python實(shí)現(xiàn)自定義包的實(shí)例詳解

    Python實(shí)現(xiàn)自定義包的實(shí)例詳解

    這篇文章主要介紹了實(shí)現(xiàn)自定義包的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-12-12
  • 分享8?個(gè)常用pandas的?index設(shè)置

    分享8?個(gè)常用pandas的?index設(shè)置

    這篇文章主要介紹了分享8?個(gè)常用pandas的?index設(shè)置,pandas?中的?index?是行索引或行標(biāo)簽。行標(biāo)簽可以說(shuō)是?pandas?的靈魂一簽,支撐了?pandas?很多強(qiáng)大的業(yè)務(wù)功能,比如多個(gè)數(shù)據(jù)框的?join,?merge?操作,自動(dòng)對(duì)齊等,下面來(lái)看看文章得具體介紹吧
    2021-12-12

最新評(píng)論

望都县| 临高县| 长武县| 蓝山县| 温州市| 宁陕县| 阜新市| 潼南县| 邯郸市| 弥渡县| 如东县| 富平县| 通海县| 东乌珠穆沁旗| 安岳县| 通州市| 永吉县| 汉川市| 台南市| 临汾市| 吉隆县| 吴忠市| 宁城县| 庆阳市| 和田县| 信丰县| 公安县| 商水县| 玉门市| 长岛县| 乐清市| 景东| 克拉玛依市| 安陆市| 黔南| 克什克腾旗| 禹城市| 连江县| 河北省| 星子县| 康平县|