Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用我控制的装饰器获取Python类的所有方法?_Python_Decorator_Python Decorators - Fatal编程技术网

如何使用我控制的装饰器获取Python类的所有方法?

如何使用我控制的装饰器获取Python类的所有方法?,python,decorator,python-decorators,Python,Decorator,Python Decorators,我需要动态地用我控制的装饰器装饰所有函数。我看到了这一点,但OP“让它变得有趣”,因为它要求他们不能控制decorator源代码(当时的解决方案是解决这个问题的一些编码技巧)。我的情况不那么复杂,因为我有控制权 我的装饰器实际上只是标记,没有任何操作noop。下面是我的装饰器的基本模式(我不使用类来定义装饰器) 然后我用这些装饰器装饰函数(所有函数也用@property装饰) def type_1(f): @wraps(f) def wrapper(*args, **kwargs

我需要动态地用我控制的装饰器装饰所有函数。我看到了这一点,但OP“让它变得有趣”,因为它要求他们不能控制decorator源代码(当时的解决方案是解决这个问题的一些编码技巧)。我的情况不那么复杂,因为我有控制权

我的装饰器实际上只是标记,没有任何操作
noop
。下面是我的装饰器的基本模式(我不使用类来定义装饰器)

然后我用这些装饰器装饰函数(所有函数也用
@property
装饰)

def type_1(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return f(*args, **kwargs)

    return wrapper


def type_2(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return f(*args, **kwargs)

    return wrapper
class MyClass(object):
    @property
    @type_1
    def a(self):
        pass

    @property
    @type_1
    def b(self):
        pass

    @property
    @type_2
    def c(self):
        pass
接下来我要做的是遍历类的每个方法并进行一些过滤。在一种情况下,我希望所有函数都标记有
@property
,而在另一种情况下,我只希望
@type_1
@type_2
。获取使用
@property
修饰的所有函数的方法已经存在

如何获取/筛选所有用
@type_1
@type_2
修饰的函数