【问题标题】:Python functools.wraps equivalent for classesPython functools.wraps 等价于类
【发布时间】:2011-09-17 16:20:14
【问题描述】:

使用类定义装饰器时,如何自动传递__name____module____doc__?通常,我会使用来自 functools 的 @wraps 装饰器。这是我为一个类所做的(这不完全是我的代码):

class memoized:
    """Decorator that caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned, and
    not re-evaluated.
    """
    def __init__(self, func):
        super().__init__()
        self.func = func
        self.cache = {}

    def __call__(self, *args):
        try:
            return self.cache[args]
        except KeyError:
            value = self.func(*args)
            self.cache[args] = value
            return value
        except TypeError:
            # uncacheable -- for instance, passing a list as an argument.
            # Better to not cache than to blow up entirely.
            return self.func(*args)

    def __repr__(self):
        return self.func.__repr__()

    def __get__(self, obj, objtype):
        return functools.partial(self.__call__, obj)

    __doc__ = property(lambda self:self.func.__doc__)
    __module__ = property(lambda self:self.func.__module__)
    __name__ = property(lambda self:self.func.__name__)

是否有标准的装饰器来自动创建名称模块和文档?另外,为了自动化 get 方法(我假设那是为了创建绑定方法?)是否缺少任何方法?

【问题讨论】:

    标签: python decorator


    【解决方案1】:

    我不知道 stdlib 中有这样的东西,但如果需要,我们可以创建自己的。

    这样的东西可以工作:

    from functools import WRAPPER_ASSIGNMENTS
    
    
    def class_wraps(cls):
        """Update a wrapper class `cls` to look like the wrapped."""
    
        class Wrapper(cls):
            """New wrapper that will extend the wrapper `cls` to make it look like `wrapped`.
    
            wrapped: Original function or class that is beign decorated.
            assigned: A list of attribute to assign to the the wrapper, by default they are:
                 ['__doc__', '__name__', '__module__', '__annotations__'].
    
            """
    
            def __init__(self, wrapped, assigned=WRAPPER_ASSIGNMENTS):
                self.__wrapped = wrapped
                for attr in assigned:
                    setattr(self, attr, getattr(wrapped, attr))
    
                super().__init__(wrapped)
    
            def __repr__(self):
                return repr(self.__wrapped)
    
        return Wrapper
    

    用法:

    @class_wraps
    class memoized:
        """Decorator that caches a function's return value each time it is called.
        If called later with the same arguments, the cached value is returned, and
        not re-evaluated.
        """
    
        def __init__(self, func):
            super().__init__()
            self.func = func
            self.cache = {}
    
        def __call__(self, *args):
            try:
                return self.cache[args]
            except KeyError:
                value = self.func(*args)
                self.cache[args] = value
                return value
            except TypeError:
                # uncacheable -- for instance, passing a list as an argument.
                # Better to not cache than to blow up entirely.
                return self.func(*args)
    
        def __get__(self, obj, objtype):
            return functools.partial(self.__call__, obj)
    
    
    @memoized
    def fibonacci(n):
        """fibonacci docstring"""
        if n in (0, 1):
           return n
        return fibonacci(n-1) + fibonacci(n-2)
    
    
    print(fibonacci)
    print("__doc__: ", fibonacci.__doc__)
    print("__name__: ", fibonacci.__name__)
    

    输出:

    <function fibonacci at 0x14627c0>
    __doc__:  fibonacci docstring
    __name__:  fibonacci
    

    编辑:

    如果你想知道为什么它没有包含在标准库中是因为你可以 将您的类装饰器包装在函数装饰器中并像这样使用functools.wraps

    def wrapper(f):
    
        memoize = memoized(f)
    
        @functools.wraps(f)
        def helper(*args, **kws):
            return memoize(*args, **kws)
    
        return helper
    
    
    @wrapper
    def fibonacci(n):
        """fibonacci docstring"""
        if n <= 1:
           return n
        return fibonacci(n-1) + fibonacci(n-2)
    

    【讨论】:

    • 谢谢穆阿德。你知道__get__方法的目的是什么吗?
    • 哦,我明白了:它使装饰器与方法一起工作?那么它可能应该在 class_wraps 中?
    • @Neil:是的更多细节:stackoverflow.com/questions/5469956/…,IMO 我不这么认为,因为它会违反我相信的功能或类的原则之一,这是 独特的责任 ,在class_wraps 的情况下将是更新一个包装器类,使其看起来像被包装的。 不多不少:)
    • @mouad:非常感谢。如果您不介意,我还有几个问题(对于您或其他任何人): 1. 我们是否希望为所有“可调用类”装饰器覆盖__get__ 是不是真的? 2.为什么我们使用functools.partial而不是返回一个与types.MethodType(self.__call__, obj)绑定的方法?
    • @Neil: 1. 是的,如果您希望能够像您已经说过的那样装饰方法(而不仅仅是函数),我坚信实现_get__ 也是一个好习惯类装饰器的方法,这样之后就不会出现任何奇怪的问题:) 2. 我认为这只是一个偏好问题the beauty is in the eye of the beholder 对,我更喜欢在这种情况下使用functools.partial,而且大多数情况下我使用types.* 进行测试对象的类型,希望我能回答你的问题:)
    【解决方案2】:

    使用继承的另一种解决方案:

    import functools
    import types
    
    class CallableClassDecorator:
        """Base class that extracts attributes and assigns them to self.
    
        By default the extracted attributes are:
             ['__doc__', '__name__', '__module__'].
        """
    
        def __init__(self, wrapped, assigned=functools.WRAPPER_ASSIGNMENTS):
            for attr in assigned:
                setattr(self, attr, getattr(wrapped, attr))
            super().__init__()
    
        def __get__(self, obj, objtype):
            return types.MethodType(self.__call__, obj)
    

    还有,用法:

    class memoized(CallableClassDecorator):
        """Decorator that caches a function's return value each time it is called.
        If called later with the same arguments, the cached value is returned, and
        not re-evaluated.
        """
        def __init__(self, function):
            super().__init__(function)
            self.function = function
            self.cache = {}
    
        def __call__(self, *args):
            try:
                return self.cache[args]
            except KeyError:
                value = self.function(*args)
                self.cache[args] = value
                return value
            except TypeError:
                # uncacheable -- for instance, passing a list as an argument.
                # Better to not cache than to blow up entirely.
                return self.function(*args)
    

    【讨论】:

    • 你不应该使用它的原因是,正如你所展示的,你必须调用父类的__init__ 方法(不一定只是super();你应该谷歌搜索@987654327 @)。
    • @ninjagecko:不应该由超类调用其他父类的__init__方法吗?
    • 这有点悬而未决,据我所知,我可能是错的。 fuhm.net/super-harmful 另外stackoverflow.com/questions/1385759/… 似乎没有任何共识。
    • @ninjagecko:是的,我已经阅读了第一篇文章。我一直在做的是总是从每个班级调用 super().__init__ 无论如何。这样,只要我继承的每个人都这样做,我就可以指望所有 __init__ 方法被调用。不幸的是,我发现 PyQt 类不这样做。我真的认为这就是合作继承的工作方式,但从你所说的听起来我可能是唯一的一个!
    【解决方案3】:

    我们真正需要做的就是修改装饰器的行为,使其“卫生”,即保留属性。

    #!/usr/bin/python3
    
    def hygienic(decorator):
        def new_decorator(original):
            wrapped = decorator(original)
            wrapped.__name__ = original.__name__
            wrapped.__doc__ = original.__doc__
            wrapped.__module__ = original.__module__
            return wrapped
        return new_decorator
    

    这就是你所需要的。一般来说。它不保留签名,但如果你真的想要,你可以使用库来做到这一点。我还继续重写了记忆代码,以便它也适用于关键字参数。还有一个错误,即未能将其转换为可散列元组会使其在 100% 的情况下无法正常工作。

    重写 memoized 装饰器的演示,@hygienic 修改其行为。 memoized 现在是一个包装原始类的函数,尽管您可以(像其他答案一样)编写一个包装类,或者更好的是,它可以检测它是否是一个类,如果是,则包装 __init__ 方法。

    @hygienic
    class memoized:
        def __init__(self, func):
            self.func = func
            self.cache = {}
    
        def __call__(self, *args, **kw):
            try:
                key = (tuple(args), frozenset(kw.items()))
                if not key in self.cache:
                    self.cache[key] = self.func(*args,**kw)
                return self.cache[key]
            except TypeError:
                # uncacheable -- for instance, passing a list as an argument.
                # Better to not cache than to blow up entirely.
                return self.func(*args,**kw)
    

    在行动:

    @memoized
    def f(a, b=5, *args, keyword=10):
        """Intact docstring!"""
        print('f was called!')
        return {'a':a, 'b':b, 'args':args, 'keyword':10}
    
    x=f(0)  
    #OUTPUT: f was called!
    print(x)
    #OUTPUT: {'a': 0, 'b': 5, 'keyword': 10, 'args': ()}                 
    
    y=f(0)
    #NO OUTPUT - MEANS MEMOIZATION IS WORKING
    print(y)
    #OUTPUT: {'a': 0, 'b': 5, 'keyword': 10, 'args': ()}          
    
    print(f.__name__)
    #OUTPUT: 'f'
    print(f.__doc__)
    #OUTPUT: 'Intact docstring!'
    

    【讨论】:

    • @hygienic 不适用于包装装饰器类具有类属性的代码。 Mouad 的解决方案虽然有效。报告的问题是:AttributeError: 'function' object has no attribute 'level' 尝试在 __call__ 内执行 decoratorclassname.level += 1
    【解决方案4】:

    似乎每个人都错过了明显的解决方案。

    >>> import functools
    >>> class memoized(object):
        """Decorator that caches a function's return value each time it is called.
        If called later with the same arguments, the cached value is returned, and
        not re-evaluated.
        """
        def __init__(self, func):
            self.func = func
            self.cache = {}
            functools.update_wrapper(self, func)  ## TA-DA! ##
        def __call__(self, *args):
            pass  # Not needed for this demo.
    
    >>> @memoized
    def fibonacci(n):
        """fibonacci docstring"""
        pass  # Not needed for this demo.
    
    >>> fibonacci
    <__main__.memoized object at 0x0156DE30>
    >>> fibonacci.__name__
    'fibonacci'
    >>> fibonacci.__doc__
    'fibonacci docstring'
    

    【讨论】:

    • __name____doc__ 是在 instance 上设置的,而不是在 help(instance) 始终使用的类上。要解决此问题,不能使用基于类的装饰器实现,而必须将装饰器实现为函数。详情见stackoverflow.com/a/25973438/1988505
    • 我不知道为什么我的答案昨天突然被标记了。没有人问过如何让 help() 工作。在 3.5 中,inspect.signature() 和 inspect.from_callable() 有了一个新的 follow_wrapped 选项;也许 help() 也应该这样做?
    • 幸运的是,ipython 的 fibonacci? 确实显示了包装器中的 doc 和 memoized 类,因此您可以同时获得两者
    • 这不会产生可腌制的类装饰器
    【解决方案5】:

    我需要一些可以同时包装类和函数的东西,于是我写了这个:

    def wrap_is_timeout(base):
        '''Adds `.is_timeout=True` attribute to objects returned by `base()`.
    
        When `base` is class, it returns a subclass with same name and adds read-only property.
        Otherwise, it returns a function that sets `.is_timeout` attribute on result of `base()` call.
    
        Wrappers make best effort to be transparent.
        '''
        if inspect.isclass(base):
            class wrapped(base):
                is_timeout = property(lambda _: True)
    
            for k in functools.WRAPPER_ASSIGNMENTS:
                v = getattr(base, k, _MISSING)
                if v is not _MISSING:
                    try:
                        setattr(wrapped, k, v)
                    except AttributeError:
                        pass
            return wrapped
    
        @functools.wraps(base)
        def fun(*args, **kwargs):
            ex = base(*args, **kwargs)
            ex.is_timeout = True
            return ex
        return fun
    

    【讨论】:

    • 旁注,我邀请大家使用这个.is_timeout=True 成语来标记你的超时导致的错误并接受来自其他包的这个API。
    【解决方案6】:

    原来有一个使用functools.wraps 本身的简单解决方案:

    import functools
    
    def dec(cls):
        @functools.wraps(cls, updated=())
        class D(cls):
            decorated = 1
        return D
    
    
    @dec
    class C:
        """doc"""
    
    print(f'{C.__name__=} {C.__doc__=} {C.__wrapped__=}')
    
    $ python3 t.py 
    C.__name__='C' C.__doc__='doc' C.__wrapped__=<class '__main__.C'>
    

    请注意,需要updated=() 以防止尝试更新类的__dict__(此输出没有updated=()):

    $ python t.py
    Traceback (most recent call last):
      File "t.py", line 26, in <module>
        class C:
      File "t.py", line 20, in dec
        class D(cls):
      File "/usr/lib/python3.8/functools.py", line 57, in update_wrapper
        getattr(wrapper, attr).update(getattr(wrapped, attr, {}))
    AttributeError: 'mappingproxy' object has no attribute 'update'
    

    【讨论】:

    • 我不明白这与我的问题有什么关系。如何将memoized 实现为一个类并提供包装功能?
    • @NeilG 如果它让你感觉更好,这至少回答了 my 问题,它映射到 OP 问题的标题,而不是具体的 memoize 示例。
    • 这解决了我在 functools.wraps 中搜索类的问题
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2010-10-12
    • 2012-08-16
    • 2011-02-04
    • 2011-02-11
    • 2015-01-25
    • 2013-04-20
    相关资源
    最近更新 更多