• <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) 閱讀(374) 評(píng)論(0)  編輯 收藏 引用


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


            99久久www免费人成精品| 久久97久久97精品免视看秋霞| 久久人人爽人人爽人人片AV麻豆 | 久久综合五月丁香久久激情| 伊人久久综合精品无码AV专区| 中文字幕无码精品亚洲资源网久久| 亚洲国产精品综合久久一线| yy6080久久| 久久精品a亚洲国产v高清不卡| AV狠狠色丁香婷婷综合久久| 久久精品国产一区二区| 18禁黄久久久AAA片| 亚洲国产精品无码久久SM| 久久精品国产福利国产秒| 精品国产乱码久久久久久浪潮| 久久久精品人妻无码专区不卡 | 99久久国产宗和精品1上映| 久久夜色精品国产欧美乱| 久久久中文字幕| 国产69精品久久久久久人妻精品| 无码AV波多野结衣久久| 狠狠精品久久久无码中文字幕| 国产精品久久久久久久久久影院 | 久久久久人妻一区精品果冻| 午夜视频久久久久一区 | 亚洲午夜久久久| 久久777国产线看观看精品| 欧美亚洲日本久久精品| 99久久无码一区人妻a黑| 色青青草原桃花久久综合| 亚洲国产精久久久久久久| 国内精品伊人久久久久777| 国产精品成人99久久久久 | 日本精品久久久久中文字幕| 久久精品女人天堂AV麻| 国产精品久久久久久一区二区三区| 久久乐国产精品亚洲综合| 色综合久久精品中文字幕首页| 久久婷婷五月综合成人D啪| 久久精品国产免费一区| 国产精品对白刺激久久久|