python - 使用类作为装饰器时,为什么 'self' 不在 args 中?

标签 python methods decorator

这个问题在这里已经有了答案:





How can I decorate an instance method with a decorator class?

(2 个回答)


上个月关闭。




Class method decorator with self arguments? 的最佳答案(答案的直接链接:https://stackoverflow.com/a/11731208)描述了装饰器在装饰方法时如何访问对象的属性。
我试图将其应用到我自己的代码中,但意识到它仅在使用函数作为装饰器时才有效。出于某种原因,当使用类作为装饰器时,参数元组不再包含 self目的。为什么会发生这种情况,是否有其他方法可以访问包装方法的 self ?
展示:

import functools


class DecoratorClass:
    def __init__(self, func):
        functools.update_wrapper(self, func)
        self.func = func                    
                        
    def __call__(self, *args, **kwargs):
        print("args:", args)            
        print("kwargs:", kwargs)
        print("Counting with:", args[0].name)
        self.func(*args, **kwargs)           
                                  

def decorator_func(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        print("args:", args)     
        print("kwargs:", kwargs)
        print("Counting with:", args[0].name)
        func(*args, **kwargs)                
    return wrapper           
                  

class Counter:
    def __init__(self, name, count):
        self.name = name            
        self.count = count
                          
    @decorator_func
    def count_a(self):
        for i in range(self.count):
            print(i)               
                    
    @DecoratorClass
    def count_b(self):
        for i in range(self.count):
            print(i)               
                    

c = Counter('my counter', 3)
                            
c.count_a()
print()    
c.count_b()
输出:
args: (<__main__.Counter object at 0x7f4491a21970>,)
kwargs: {}
Counting with: my counter
0
1
2

args: ()
kwargs: {}
Traceback (most recent call last):
  File "./test2.py", line 48, in <module>
    c.count_b()
  File "./test2.py", line 14, in __call__
    print("Counting with:", args[0].name)
IndexError: tuple index out of range

最佳答案

这是一个已知问题(请参阅 here )。不久前,在我的一个项目中实现类装饰器时,我实际上遇到了同样的问题。
为了解决这个问题,我在我的类(class)中添加了以下方法 - 您也可以将其添加到您的 DecoratorClass 中。 ,然后它应该都能正常工作。

def __get__(self, instance, owner):
    """
    Fix: make our decorator class a decorator, so that it also works to
    decorate instance methods.
    https://stackoverflow.com/a/30105234/10237506
    """
    from functools import partial
    return partial(self.__call__, instance)
另外,请参阅链接的 SO 答案以了解原因的示例。

关于python - 使用类作为装饰器时,为什么 'self' 不在 args 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69288996/

相关文章:

ios - 让文本在 iPhone OS 中闪烁的最佳方法?

python /Django : Adding custom model methods?

java - 如何制作以某种方式重复参数方法的java方法?

python - Python中从子对象访问父对象数据成员

python - 派生类的类/元类方法装饰器

python - TypeError:在字符串格式化python期间并非所有参数都转换了

python - BeautifulSoup 在特定标题后得到表格

python - Python 中意外的浮点表示

Python Asyncio 子进程永远不会完成

python - Python 装饰器有哪些常见用途?