Python 获取所有函数装饰器的名称

Python 获取所有函数装饰器的名称,python,python-3.x,Python,Python 3.x,我希望以编程方式将decorator应用于类的所有方法。如果某个decorator已经存在,则跳过向该方法添加decorator 我有类似的代码,但我无法找到API来告诉我该方法上可能已经存在哪些其他装饰器。在我的例子中,我想跳过为某些方法添加可缓存的decorator,这些方法上有不可缓存的decorator def cacheable(): def decorate(cls): for name, fn in inspect.getmembers(cls, insp

我希望以编程方式将decorator应用于类的所有方法。如果某个decorator已经存在,则跳过向该方法添加decorator

我有类似的代码,但我无法找到API来告诉我该方法上可能已经存在哪些其他装饰器。在我的例子中,我想跳过为某些方法添加
可缓存的
decorator,这些方法上有
不可缓存的
decorator

def cacheable():
     def decorate(cls):
        for name, fn in inspect.getmembers(cls, inspect.ismethod):
            setattr(cls, name, decorator(fn))
        return cls
    return decorate

@cacheable
class C(object):
    
    @not_cacheable
    def method_1(self): pass

    def method_2(self, x): pass
    ...

尝试添加属性:

def not_cachable(func):
    func._cache = False
    return func
然后检查
cacheable
装饰器中的属性

这就是标准库中有多少函数可以工作,例如
@abstractmethod