Python3 裝飾器,python3裝飾器

來源:互聯網
上載者:User

Python3 裝飾器,python3裝飾器

  1 '''  2 所用知識點:  3 1.範圍 :L>E>G>B  4 2.高階函數:  5     滿足兩個條件中的一個就行:  6     1.函數名可以作為參數輸入  7     2.函數名可以作為傳回值  8 3.閉包  9  10 ''' 11  12  13 ''' 14 閉包 15 python中的閉包從表現形式上定義為: 16 如果在一個內建函式裡,對在外部範圍(但不是在全域範圍)的變數進行引用, 17 那麼內建函式就被稱為閉包。 18  ''' 19 # def out1(): 20 #     x = 10 21 #     def in1():      # 內建函式 22 #         y = 1 23 #         print(x)    # 對外部範圍的變數進行引用 24 #     return in1      # 內建函式in1稱之為閉包 25 #  26 # # 調用in1() 27 # # 方法一 28 # out1()() 29 # # 方法二 30 # p = out1() 31 # p() 32 # # 33 # # in1()     # 局部變數,全域無法調用 34 # # 35 #  36 ''' 37 閉包 = 函數塊(內建函式) + 定義函數時的環境(變數引用) 38 ''' 39 #  40 # import time 41 #  42 # # start = time.time()     # 開始時間 43 # # time.sleep(1)           # 間歇時間,間歇1s 44 # # end = time.time()       # 結束時間 45 #  46 # def p1(): 47 # #    print(start)        # 從1970-01-01開始計算秒數 48 #     st1 = time.time() 49 #     print('Hello World') 50 #     time.sleep(5) 51 #     end1 = time.time() 52 #     print('花費了 %s 秒'%(end1 - st1)) 53 # p1() 54 #  55 ''' 56 。。。。。。 57 n多個類似需要計算時間的函數 58 此時為了方便起見,建立一個專門用於計算時間的函數 59 ''' 60 #  61 # def showtime(x): 62 #     start2 = time.time() 63 #     x() 64 #     end2 = time.time() 65 #     print('程式花費了 %s 秒' % (end2-start2)) 66 #  67 # def test(): 68 #     time.sleep(2) 69 #     print('Yeah') 70 #  71 # showtime(test) 72 #  73 ''' 74 裝飾器: 75 為之前的函數添加新的功能 76 ''' 77 #  78 #  79 # def showt(c): 80 #     def a(): 81 #         start2 = time.time() 82 #         c() 83 #         end2 = time.time() 84 #         print('程式花費了 %s 秒' % (end2 - start2)) 85 #     return a 86 #  87 # @showt      # 等價於z = showt(test1) 88 #             # 但要放在函數之前 89 # def test1(): 90 #     time.sleep(1) 91 #     print(123) 92 # test1() 93 #  94 #  95 ''' 96 被裝飾器修飾的參數 97 ''' 98 #  99 # def fun1(x):100 #     def in_fun1(*y,**z):101 #         start = time.time()102 #         x(*y,**z)103 #         end = time.time()104 #         print('此程式用了%s秒' % (end-start))105 #     return in_fun1106 # 107 # @fun1108 # 109 # def fun2(*a,**b):110 #     Sum = 0111 #     for i in a:112 #         Sum += i113 #     print(Sum)114 #     time.sleep(2)115 # fun2(3,5,1)116 # 117 # 118 # # 功能函數加參數119 # def loge(s=''):120 #     def fun3(q):121 #         def infun3(*x,**y):122 #             start = time.time()123 #             q(*x,**y)124 #             end = time.time()125 #             print('%s 秒' % (end-start))126 #             if  s == 'true':127 #                 print('loge !!!')128 #         return infun3129 #     return fun3130 # 131 # @loge('true')       # 中間的參數‘true’可以寫成其他的,只不過不會出來loge132 # def test3():133 #     print('test3')134 #     time.sleep(1)135 # test3()

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.