• <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 周強 閱讀(368) 評論(0)  編輯 收藏 引用

            久久中文娱乐网| 88久久精品无码一区二区毛片| 久久久久国产一区二区| 亚洲va久久久噜噜噜久久狠狠 | 国产一区二区精品久久岳| 99精品国产免费久久久久久下载| 精品久久久久久国产| 久久国产免费观看精品3| 色综合久久中文字幕综合网| 国产精品久久网| 国内精品久久久久久野外| 国产亚州精品女人久久久久久| 亚洲国产成人精品女人久久久| 亚洲伊人久久精品影院| 久久久久99这里有精品10| 婷婷久久综合九色综合九七| 久久精品欧美日韩精品| 青青青青久久精品国产h久久精品五福影院1421 | 亚洲国产精品久久久天堂| 久久99亚洲综合精品首页| 久久99精品久久只有精品| 久久国产精品成人影院| 香蕉久久AⅤ一区二区三区| 久久亚洲综合色一区二区三区| 久久婷婷国产剧情内射白浆| 欧美日韩成人精品久久久免费看| 久久精品国产亚洲AV无码麻豆| 伊人久久大香线蕉AV一区二区| 精品久久人人做人人爽综合| 青青草原综合久久| 久久综合久久综合久久| 伊人丁香狠狠色综合久久| 国产午夜久久影院| 狠狠狠色丁香婷婷综合久久五月| 精品国产乱码久久久久久1区2区| 亚洲国产精品无码久久一区二区| 久久精品国产久精国产果冻传媒| 久久福利资源国产精品999| 久久人人爽人人爽人人av东京热| 久久人人爽人人爽人人片AV高清| 伊人久久大香线蕉综合5g|