• <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久久无色码中文字幕人妻| 国产精品嫩草影院久久| 久久久久国产精品嫩草影院| 日韩乱码人妻无码中文字幕久久| 伊人久久大香线蕉AV一区二区| 国产精品对白刺激久久久| 久久国产亚洲精品麻豆| 久久久久亚洲爆乳少妇无| 无码人妻少妇久久中文字幕蜜桃| 久久精品国产清自在天天线 | 久久久噜噜噜久久| 一本大道久久东京热无码AV | 五月丁香综合激情六月久久| 人妻无码αv中文字幕久久琪琪布| 国产精品久久亚洲不卡动漫| 久久婷婷五月综合97色直播| 国产欧美一区二区久久| 国色天香久久久久久久小说 | 国产精品毛片久久久久久久| 精品熟女少妇aⅴ免费久久| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 中文字幕亚洲综合久久2| 无码人妻精品一区二区三区久久久 | 国产精品99久久久精品无码| 精品久久久久久国产潘金莲| 久久久久久伊人高潮影院| 久久人妻少妇嫩草AV蜜桃| 韩国三级中文字幕hd久久精品| 久久九九全国免费| 久久久久免费看成人影片| 18岁日韩内射颜射午夜久久成人 | 国产激情久久久久久熟女老人 | 无码精品久久久天天影视 | 日本三级久久网| 久久国产亚洲高清观看| 2019久久久高清456| 久久久国产视频| 国产免费久久精品99re丫y| 精品久久久久成人码免费动漫| 久久99久久99精品免视看动漫|