python裝飾器(補充完整)

來源:互聯網
上載者:User

標籤:不用   func   解釋   %s   方法   完整   哈哈哈   裝飾器   意思   

       一、什麼是裝飾器               1、對現有已經實現功能的代碼進行擴充。              2、不用修改被撞時對象的原始碼和調用方法                 二、裝飾器的使用             無參數裝飾器:            樣本:        
           def zsq(func):                          def yz(args):                                print("驗證")                                return func(args)                          return yz                    @zsq                    def hs(name):                          print("我是一個函數%s"%name)                          return "hs fanhuizhi"                     ceshi = hs("wz")                    print(ceshi)                     執行過程:              1、將zsq載入到記憶體中。              2、碰到@zsq,會執行zsq函數將@zaq下的函數當作參數傳入zsq中(相當於zsq(hs))。              3、此時func形參等於hs函數,讀取yz函數到記憶體,return yz函數的記憶體位址到hs函數(@zsq就相當於(hs = zsq(hs)))。              4、此時執行ceshi = hs(“wz”),由於hs被return 回來的yz函數覆蓋,這裡執行的就相當於ceshi = yz(“wz”)。              5、執行yz函數,首先print(“驗證”),然後return func(args),這句return func(args)的意思是執行func函數並把函數的傳回值返回給調用yz函數的對象,而調用yz函數的為ceshi = hs(“wz”),所以這一步就是把傳回值傳給hs(“wz”)並賦值給ceshi變數。              6、在這裡執行func(args)由於第三步的時候func等於hs函數所以就相當於執行hs函數。       

  

    有參數裝飾器:              樣本:                              def hs1(canshu1, canshu2):              print("第一個函數")        def hs2(canshu1, canshu2):              print("第二個函數")        def zsq(hs_1, hs_2):              def home(home_func):                    def panduan(pd1, pd2):                          hs1_return = hs_1(pd1, pd2)                          if (hs1_return != None):                                return hs1_return                          home_return = home_func(pd1, pd2)                          if (home_return != None):                                return home_return                          hs2_return = hs_2(pd1, pd2)                          if (hs2_return != None):                                return hs2_return                    return panduan              return home         @zsq(hs1, hs2)        def index(canshu1, canshu2):              print("index")         index(‘w‘,‘z‘)                #執行過程              1、解譯器從上往下解釋python代碼首先將hs1函數載入到記憶體,然後把hs2和zsq函數也加在到記憶體。              2、碰到@zsq(hs1, hs2),解譯器首先會執行zsq(hs1, hs2),把hs1和hs2當作參數傳給zsq賦值給zsq的形參hs_1和hs_2(這裡相當於@zsq = zsq(hs1,hs2))。              3、將home函數載入到記憶體並return給@zsq。              4、執行裝飾器將index函數當作值傳給zsq函數,由於@zsq已被home函數覆蓋這時候相當於執行index = home(index)。              5、讀取panduan函數到記憶體中並把panduan函數的記憶體位址返回給調用home函數的index中。              6、執行index(‘w’,’z’),由於index被panduan覆蓋這裡就相當於執行panduan(‘w’,’z’)。              7、執行panduan函數中的代碼(不再贅述…)            三、裝飾器文法:              被裝飾函數的正上方,單獨一行                    @deco1                    @deco2                    @deco3                    def foo():                        pass                     foo=deco1(deco2(deco3(foo)))        四、裝飾器補充:wraps              from functools import wraps              def deco(func):                    @wraps(func) #加在最內層函數正上方                    def wrapper(*args,**kwargs):                          return func(*args,**kwargs)                          return wrapper              @deco              def  index():                    ‘‘‘哈哈哈哈‘‘‘                    print(‘from index‘)              print(index.__doc__)  

python裝飾器(補充完整)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.