Functools.update_wrapper() doesn't work properly

不打扰是莪最后的温柔 提交于 2019-12-05 22:17:13

问题


I use Functools.update_wrapper() in my decorator, but It seems like update_wrapper rewrites only function attributes (such as __doc__, __name__), but does not affect on help() function.

I aware of these answers, but they don't work with decorator-class.

Here is my function.

import functools

class memoized(object):

    def __init__(self, func):
        self.func = func
        functools.update_wrapper(self, func)

    def __call__(self, *args):
        self.func(*args)

@memoized 
def printer(arg):
    "This is my function"
    print arg

Here is the output

>>> printer.__doc__
This is my function

>>> help(printer)
Help on memoized in module __main__ object:

printer = class memoized(__builtin__.object)
 |  Methods defined here:
 |  
 |  __call__(self, *args)
 |  
 |  __init__(self, func)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

It looks like a bug, but how can I fix it?


回答1:


functools.update_wrapper() sets the attribute on the instance, but help() looks at the information on the type.

So printer.__doc__ gives you the instance attribute, help() prints information about type(printer), e.g. the memoized class, which does not have a __doc__ attribute.

This is not a bug, this is all by design; help() will always look at the class when you pass in an instance. Don't use a class as decorator if you want help() to work for the decorated function.



来源:https://stackoverflow.com/questions/25973376/functools-update-wrapper-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!