python wraps裝飾器

來源:互聯網
上載者:User

標籤:

這是一個很有用的裝飾器。看過前一篇反射的朋友應該知道,函數是有幾個特殊屬性比如函數名,在被裝飾後,上例中的函數名foo會變成封裝函數的名字 wrapper,如果你希望使用反射,可能會導致意外的結果。這個裝飾器可以解決這個問題,它能將裝飾過的函數的特殊屬性保留。

import timeimport functools def timeit(func):    @functools.wraps(func)    def wrapper():        start = time.clock()        func()        end =time.clock()        print ‘used:‘, end - start    return wrapper @timeitdef foo():    print ‘in foo()‘ foo()print foo.__name__

首先注意第5行,如果注釋這一行,foo.__name__將是‘wrapper‘。另外相信你也注意到了,這個裝飾器竟然帶有一個參數。實際上,他還有 另外兩個可選的參數,assigned中的屬性名稱將使用賦值的方式替換,而updated中的屬性名稱將使用update的方式合并,你可以通過查看 functools的原始碼獲得它們的預設值。對於這個裝飾器,相當於wrapper = functools.wraps(func)(wrapper)。

 # -*- coding=utf-8 -*- from functools import wraps   def my_decorator(func):    @wraps(func)    def wrapper(*args, **kwargs):        print(‘Calling decorated function...‘)        return func(*args, **kwargs)    return wrapper  

@my_decorator def example(): """Docstring""" print(‘Called example function‘)print(example.__name__, example.__doc__)

列印出:example Docstring

python wraps裝飾器

聯繫我們

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