• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Just enjoy programming

            python functools wraps (轉(zhuǎn))

            When you use a decorator, you're replacing one function with another. In other words, if you have a decorator

            1. def logged(func):  
            2.     def with_logging(*args, **kwargs):  
            3.         print func.__name__ + " was called"  
            4.         return func(*args, **kwargs)  
            5.     return with_logging  

            then when you say

            1. @logged  
            2. def f(x):  
            3.    """does some math"""  
            4.    return x + x * x  

            it's exactly the same as saying

            1. def f(x):  
            2.     """does some math"""  
            3.     return x + x * x  
            4. f = logged(f)  

            and your function f is replaced with the function with_logging. Unfortunately, this means that if you then say

            1. print f.__name__  

            it will print with_logging because that's the name of your new function. In fact, if you look at the docstring for f, it will be blank because with_logging has no docstring, and so the docstring you wrote won't be there anymore. Also, if you look at the pydoc result for that function, it won't be listed as taking one argument x; instead it'll be listed as taking *args and **kwargs because that's what with_logging takes.

            If using a decorator always meant losing this information about a function, it would be a serious problem. That's why we have functools.wraps. This takes a function used in a decorator and adds the functionality of copying over the function name, docstring, arguments list, etc. And since wraps is itself a decorator, the following code does the correct thing:

            1. from functools import wraps  
            2. def logged(func):  
            3.     @wraps(func)  
            4.     def with_logging(*args, **kwargs):  
            5.         print func.__name__ + " was called"  
            6.         return func(*args, **kwargs)  
            7.     return with_logging  
            8.  
            9. @logged  
            10. def f(x):  
            11.    """does some math"""  
            12.    return x + x * x  
            13.   
            14. print f.__name__  # prints 'f'  
            15. print f.__doc__   # prints 'does some math'  


              轉(zhuǎn)自http://blog.csdn.net/wanghai__/article/details/7078792

            posted on 2013-11-05 20:40 周強(qiáng) 閱讀(368) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            97久久国产亚洲精品超碰热 | 国产精品激情综合久久| 久久人妻无码中文字幕| 亚洲一级Av无码毛片久久精品| 久久久久久久尹人综合网亚洲| 久久久久人妻一区精品性色av| 色婷婷综合久久久久中文| 日韩精品久久无码中文字幕| 无码人妻久久一区二区三区免费| 亚洲国产另类久久久精品| 久久久精品人妻一区二区三区蜜桃 | 国产一久久香蕉国产线看观看 | 欧美性大战久久久久久| 无码乱码观看精品久久| 国产精品中文久久久久久久| 国内精品伊人久久久久妇| 亚洲国产精品无码久久久蜜芽| 蜜臀av性久久久久蜜臀aⅴ| 久久久亚洲欧洲日产国码aⅴ| 国产精品久久久久久一区二区三区| 国内精品久久久久影院免费| 国产成人久久久精品二区三区| 日本亚洲色大成网站WWW久久| 久久天天婷婷五月俺也去| 人妻精品久久久久中文字幕69| 久久九九亚洲精品| 久久久无码精品午夜| 一本一本久久aa综合精品| 97久久精品人人做人人爽| 无码人妻少妇久久中文字幕 | 久久综合视频网站| 亚洲中文久久精品无码ww16| 国产美女久久精品香蕉69| 精品无码久久久久久国产| 国产精品久久久久久五月尺| 国产精品久久久久久搜索| 久久天天躁夜夜躁狠狠| A级毛片无码久久精品免费| 国产成人精品综合久久久| 国产激情久久久久影院老熟女| 亚洲精品国产字幕久久不卡|