• <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 (轉)

            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'  


              轉自http://blog.csdn.net/wanghai__/article/details/7078792

            posted on 2013-11-05 20:40 周強 閱讀(374) 評論(0)  編輯 收藏 引用

            99久久免费国产精品| 久久综合亚洲鲁鲁五月天| 国产精品久久久久国产A级| 久久久久亚洲AV片无码下载蜜桃| 亚洲AV无码一区东京热久久| 国产精品一久久香蕉国产线看观看| 久久久精品免费国产四虎| 久久精品国产亚洲5555| 色婷婷综合久久久久中文 | 伊人久久大香线蕉综合Av| 狼狼综合久久久久综合网| 久久精品国产精品亚洲人人| 日日躁夜夜躁狠狠久久AV| 久久97久久97精品免视看| 久久精品国产亚洲AV麻豆网站| 久久天天躁狠狠躁夜夜2020 | 久久精品aⅴ无码中文字字幕不卡| 777米奇久久最新地址| 亚洲国产精品成人AV无码久久综合影院| 久久久久亚洲av无码专区喷水| 久久亚洲2019中文字幕| 国产精品一区二区久久精品无码| 狠狠色丁香婷婷久久综合| 91精品日韩人妻无码久久不卡| 久久人人爽人人爽人人AV东京热| 国产69精品久久久久观看软件| 国产免费福利体检区久久| 国产亚洲色婷婷久久99精品| 婷婷久久综合九色综合九七| 丰满少妇人妻久久久久久4| 99久久国产综合精品麻豆| 久久精品人成免费| 久久精品国产亚洲av麻豆小说 | 一极黄色视频久久网站| 久久99精品免费一区二区| 亚洲一区二区三区日本久久九| 国产一区二区三区久久精品| 2020久久精品国产免费| 久久综合九色综合精品| 久久国产精品-国产精品| 国产99久久久久久免费看|