• <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)  編輯 收藏 引用

            国产精品va久久久久久久| 国内精品伊人久久久久妇| 久久夜色tv网站| 国产午夜精品久久久久九九| 人妻系列无码专区久久五月天| 久久93精品国产91久久综合| 久久丫忘忧草产品| 国内精品久久人妻互换| 99久久无码一区人妻| 一本综合久久国产二区| 久久精品国产99久久久| 久久伊人色| 国产精品美女久久久久| 亚洲欧美国产日韩综合久久| 国产午夜免费高清久久影院| 亚洲精品国产综合久久一线| av无码久久久久久不卡网站| 亚洲欧美一级久久精品| 久久亚洲国产精品一区二区| 777午夜精品久久av蜜臀| 国产成人无码精品久久久免费| 久久经典免费视频| 久久国产视屏| A狠狠久久蜜臀婷色中文网| 国产精品久久久久久久app| 国产69精品久久久久99尤物| 久久久无码精品亚洲日韩蜜臀浪潮| 欧美成a人片免费看久久| 人人狠狠综合久久亚洲88| 久久水蜜桃亚洲av无码精品麻豆| 久久久久无码精品| 91精品国产91久久久久久青草| 国产午夜精品久久久久免费视| 久久天天婷婷五月俺也去| 久久亚洲中文字幕精品一区四| 一级做a爱片久久毛片| 97久久天天综合色天天综合色hd| 亚洲香蕉网久久综合影视| 久久人妻AV中文字幕| 奇米影视7777久久精品人人爽| 色婷婷久久综合中文久久一本|