如何使用给定的装饰器获取python类的所有方法

kra*_*aiz 74 python methods class decorator inspect

如何获取用@ decorator2装饰的给定类A的所有方法?

class A():
    def method_a(self):
      pass

    @decorator1
    def method_b(self, b):
      pass

    @decorator2
    def method_c(self, t=5):
      pass
Run Code Online (Sandbox Code Playgroud)

nin*_*cko 107

方法1:基本注册装饰器

我已在这里回答了这个问题:在Python中通过数组索引调用函数 =)


方法2:源代码解析

如果您无法控制定义,这是您想要的一种解释,这是不可能的(没有代码读取 - 反射),因为例如装饰器可能是一个无操作装饰器(如在我的链接示例中)仅返回未修改的函数.(尽管如果你允许自己包装/重新定义装饰器,请参阅方法3:将装饰器转换为"自我意识",然后你会找到一个优雅的解决方案)

这是一个可怕的糟糕黑客,但您可以使用该inspect模块来读取源代码本身,并解析它.这在交互式解释器中不起作用,因为inspect模块将拒绝以交互模式提供源代码.但是,下面是概念证明.

#!/usr/bin/python3

import inspect

def deco(func):
    return func

def deco2():
    def wrapper(func):
        pass
    return wrapper

class Test(object):
    @deco
    def method(self):
        pass

    @deco2()
    def method2(self):
        pass

def methodsWithDecorator(cls, decoratorName):
    sourcelines = inspect.getsourcelines(cls)[0]
    for i,line in enumerate(sourcelines):
        line = line.strip()
        if line.split('(')[0].strip() == '@'+decoratorName: # leaving a bit out
            nextLine = sourcelines[i+1]
            name = nextLine.split('def')[1].split('(')[0].strip()
            yield(name)
Run Code Online (Sandbox Code Playgroud)

有用!:

>>> print(list(  methodsWithDecorator(Test, 'deco')  ))
['method']
Run Code Online (Sandbox Code Playgroud)

请注意,必须注意解析和python语法,例如@deco并且@deco(...是有效的结果,但@deco2如果我们只是要求,则不应该返回'deco'.我们注意到根据http://docs.python.org/reference/compound_stmts.html上的官方python语法,装饰器如下:

decorator      ::=  "@" dotted_name ["(" [argument_list [","]] ")"] NEWLINE
Run Code Online (Sandbox Code Playgroud)

我们松了一口气,不必处理像这样的案件@(deco).但是请注意,这仍然没有真正帮助你,如果你真的有非常复杂的装饰,例如@getDecorator(...),如

def getDecorator():
    return deco
Run Code Online (Sandbox Code Playgroud)

因此,这种解析代码的最佳解决方案无法检测到这样的情况.虽然如果你使用的是这种方法,那么你真正追求的就是在定义中的方法之上编写的内容,在本例中是getDecorator.

根据规范,@foo1.bar2.baz3(...)作为装饰者也是有效的.您可以扩展此方法以使用它.您也可以通过大量工作来扩展此方法以返回<function object ...>而不是函数的名称.然而,这种方法是hackish和可怕的.


方法3:将装饰器转换为"自我意识"

如果你没有在控制装饰定义(这是你想要什么另一种解释),那么所有这些问题消失,因为你必须在装饰是如何应用的控制.因此,您可以通过包装来修改装饰器,以创建自己的装饰器,并使用来装饰您的功能.让我再说一遍:你可以制作一个装饰器来装饰你无法控制的装饰器,"启发"它,在我们的例子中它使它做它之前做的事情,但.decorator元数据属性附加到它返回的callable ,让你跟踪"这个功能是否装饰?让我们检查function.decorator!".而且那么你可以遍历类的方法,只是检查,看看是否有装饰适当的.decorator财产!=)如下所示:

def makeRegisteringDecorator(foreignDecorator):
    """
        Returns a copy of foreignDecorator, which is identical in every
        way(*), except also appends a .decorator property to the callable it
        spits out.
    """
    def newDecorator(func):
        # Call to newDecorator(method)
        # Exactly like old decorator, but output keeps track of what decorated it
        R = foreignDecorator(func) # apply foreignDecorator, like call to foreignDecorator(method) would have done
        R.decorator = newDecorator # keep track of decorator
        #R.original = func         # might as well keep track of everything!
        return R

    newDecorator.__name__ = foreignDecorator.__name__
    newDecorator.__doc__ = foreignDecorator.__doc__
    # (*)We can be somewhat "hygienic", but newDecorator still isn't signature-preserving, i.e. you will not be able to get a runtime list of parameters. For that, you need hackish libraries...but in this case, the only argument is func, so it's not a big issue

    return newDecorator
Run Code Online (Sandbox Code Playgroud)

示范@decorator:

deco = makeRegisteringDecorator(deco)

class Test2(object):
    @deco
    def method(self):
        pass

    @deco2()
    def method2(self):
        pass

def methodsWithDecorator(cls, decorator):
    """ 
        Returns all methods in CLS with DECORATOR as the
        outermost decorator.

        DECORATOR must be a "registering decorator"; one
        can make any decorator "registering" via the
        makeRegisteringDecorator function.
    """
    for maybeDecorated in cls.__dict__.values():
        if hasattr(maybeDecorated, 'decorator'):
            if maybeDecorated.decorator == decorator:
                print(maybeDecorated)
                yield maybeDecorated
Run Code Online (Sandbox Code Playgroud)

有用!:

>>> print(list(   methodsWithDecorator(Test2, deco)   ))
[<function method at 0x7d62f8>]
Run Code Online (Sandbox Code Playgroud)

但是,"注册装饰器"必须是最外面的装饰器,否则.decorator属性注释将丢失.例如在火车上

@decoOutermost
@deco
@decoInnermost
def func(): ...
Run Code Online (Sandbox Code Playgroud)

decoOutermost除非我们保留对"更多内部"包装器的引用,否则您只能看到公开的元数据.

旁注:上面的方法也可以构建一个.decorator跟踪应用装饰器和输入函数以及装饰器工厂参数整个堆栈的方法.=)例如,如果考虑注释掉的行R.original = func,可以使用这样的方法来跟踪所有包装层.如果我写了一个装饰库,这就是我个人所做的,因为它允许深入反省.

@foo和之间也有区别@bar(...).虽然它们都是规范中定义的"装饰者表达",但请注意它foo是装饰器,同时bar(...)返回动态创建的装饰器,然后应用它.因此,您需要一个单独的功能makeRegisteringDecoratorFactory,有点像makeRegisteringDecorator甚至更多META:

def makeRegisteringDecoratorFactory(foreignDecoratorFactory):
    def newDecoratorFactory(*args, **kw):
        oldGeneratedDecorator = foreignDecoratorFactory(*args, **kw)
        def newGeneratedDecorator(func):
            modifiedFunc = oldGeneratedDecorator(func)
            modifiedFunc.decorator = newDecoratorFactory # keep track of decorator
            return modifiedFunc
        return newGeneratedDecorator
    newDecoratorFactory.__name__ = foreignDecoratorFactory.__name__
    newDecoratorFactory.__doc__ = foreignDecoratorFactory.__doc__
    return newDecoratorFactory
Run Code Online (Sandbox Code Playgroud)

示范@decorator(...):

def deco2():
    def simpleDeco(func):
        return func
    return simpleDeco

deco2 = makeRegisteringDecoratorFactory(deco2)

print(deco2.__name__)
# RESULT: 'deco2'

@deco2()
def f():
    pass
Run Code Online (Sandbox Code Playgroud)

这个生成器 - 工厂包装器也可以工作:

>>> print(f.decorator)
<function deco2 at 0x6a6408>
Run Code Online (Sandbox Code Playgroud)

奖励让我们甚至尝试使用方法#3:

def getDecorator(): # let's do some dispatching!
    return deco

class Test3(object):
    @getDecorator()
    def method(self):
        pass

    @deco2()
    def method2(self):
        pass
Run Code Online (Sandbox Code Playgroud)

结果:

>>> print(list(   methodsWithDecorator(Test3, deco)   ))
[<function method at 0x7d62f8>]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,与方法2不同,@ deco被正确识别,即使它从未在类中明确写入.与method2不同,如果在运行时(手动,通过元类等)添加方法或继承该方法,这也将起作用.

请注意,您还可以修饰一个类,因此如果您"启发"一个用于装饰方法和类的装饰器,然后在要分析的类的主体内编写一个类,那么methodsWithDecorator将装饰类返回为以及装饰方法.人们可以认为这是一个功能,但是您可以通过检查装饰器的参数来轻松编写逻辑来忽略它们,即.original实现所需的语义.

  • 哇!现在,这就是你回答问题的方式! (8认同)
  • 对于一个不明显的解决方案的问题,这是一个很好的答案,我已经为这个答案开了一个赏金。对不起,我没有足够的代表给你更多! (2认同)
  • @NiallDouglas:谢谢。=)(我不知道在经过大量编辑后,答案如何自动转换为“社区维基”,所以我没有得到大多数赞成票的代表......所以谢谢!) (2认同)

Jas*_*n S 17

如果您确实可以控制装饰器,则可以使用装饰器类而不是函数:

class awesome(object):
    def __init__(self, method):
        self._method = method
    def __call__(self, obj, *args, **kwargs):
        return self._method(obj, *args, **kwargs)
    @classmethod
    def methods(cls, subject):
        def g():
            for name in dir(subject):
                method = getattr(subject, name)
                if isinstance(method, awesome):
                    yield name, method
        return {name: method for name,method in g()}

class Robot(object):
   @awesome
   def think(self):
      return 0

   @awesome
   def walk(self):
      return 0

   def irritate(self, other):
      return 0

Run Code Online (Sandbox Code Playgroud)

如果我调用awesome.methods(Robot)它返回

{'think': <mymodule.awesome object at 0x000000000782EAC8>, 'walk': <mymodulel.awesome object at 0x000000000782EB00>}
Run Code Online (Sandbox Code Playgroud)


Sha*_*way 14

为了扩展@ninjagecko方法2中的优秀答案:源代码解析,ast只要inspect模块可以访问源代码,就可以使用Python 2.6中引入的模块执行自检.

def findDecorators(target):
    import ast, inspect
    res = {}
    def visit_FunctionDef(node):
        res[node.name] = [ast.dump(e) for e in node.decorator_list]

    V = ast.NodeVisitor()
    V.visit_FunctionDef = visit_FunctionDef
    V.visit(compile(inspect.getsource(target), '?', 'exec', ast.PyCF_ONLY_AST))
    return res
Run Code Online (Sandbox Code Playgroud)

我添加了一个稍微复杂的装饰方法:

@x.y.decorator2
def method_d(self, t=5): pass
Run Code Online (Sandbox Code Playgroud)

结果:

> findDecorators(A)
{'method_a': [],
 'method_b': ["Name(id='decorator1', ctx=Load())"],
 'method_c': ["Name(id='decorator2', ctx=Load())"],
 'method_d': ["Attribute(value=Attribute(value=Name(id='x', ctx=Load()), attr='y', ctx=Load()), attr='decorator2', ctx=Load())"]}
Run Code Online (Sandbox Code Playgroud)


Nic*_*ise 5

对于我们这些只想要绝对最简单的情况的人 - 即单文件解决方案,我们可以完全控制我们正在使用的类和我们正在尝试跟踪的装饰器,我有一个答案。ninjagecko 链接到了一个解决方案,当您可以控制要跟踪的装饰器时,但我个人发现它很复杂并且很难理解,可能是因为到目前为止我从未使用过装饰器。因此,我创建了以下示例,目标是尽可能简单明了。它是一个装饰器,一个具有多个装饰方法的类,以及用于检索+运行所有应用了特定装饰器的方法的代码。

# our decorator
def cool(func, *args, **kwargs):
    def decorated_func(*args, **kwargs):
        print("cool pre-function decorator tasks here.")
        return_value = func(*args, **kwargs)
        print("cool post-function decorator tasks here.")
        return return_value
    # add is_cool property to function so that we can check for its existence later
    decorated_func.is_cool = True
    return decorated_func

# our class, in which we will use the decorator
class MyClass:
    def __init__(self, name):
        self.name = name

    # this method isn't decorated with the cool decorator, so it won't show up 
    # when we retrieve all the cool methods
    def do_something_boring(self, task):
        print(f"{self.name} does {task}")
    
    @cool
    # thanks to *args and **kwargs, the decorator properly passes method parameters
    def say_catchphrase(self, *args, catchphrase="I'm so cool you could cook an egg on me.", **kwargs):
        print(f"{self.name} says \"{catchphrase}\"")

    @cool
    # the decorator also properly handles methods with return values
    def explode(self, *args, **kwargs):
        print(f"{self.name} explodes.")
        return 4

    def get_all_cool_methods(self):
        """Get all methods decorated with the "cool" decorator.
        """
        cool_methods =  {name: getattr(self, name)
                            # get all attributes, including methods, properties, and builtins
                            for name in dir(self)
                                # but we only want methods
                                if callable(getattr(self, name))
                                # and we don't need builtins
                                and not name.startswith("__")
                                # and we only want the cool methods
                                and hasattr(getattr(self, name), "is_cool")
        }
        return cool_methods

if __name__ == "__main__":
    jeff = MyClass(name="Jeff")
    cool_methods = jeff.get_all_cool_methods()    
    for method_name, cool_method in cool_methods.items():
        print(f"{method_name}: {cool_method} ...")
        # you can call the decorated methods you retrieved, just like normal,
        # but you don't need to reference the actual instance to do so
        return_value = cool_method()
        print(f"return value = {return_value}\n")
Run Code Online (Sandbox Code Playgroud)

运行上面的示例会得到以下输出:

explode: <bound method cool.<locals>.decorated_func of <__main__.MyClass object at 0x00000220B3ACD430>> ...
cool pre-function decorator tasks here.
Jeff explodes.
cool post-function decorator tasks here.
return value = 4

say_catchphrase: <bound method cool.<locals>.decorated_func of <__main__.MyClass object at 0x00000220B3ACD430>> ...
cool pre-function decorator tasks here.
Jeff says "I'm so cool you could cook an egg on me."
cool post-function decorator tasks here.
return value = None
Run Code Online (Sandbox Code Playgroud)

请注意,此示例中的修饰方法具有不同类型的返回值和不同的签名,因此能够检索和运行它们的实际价值有点可疑。但是,在有许多类似方法的情况下,所有方法都具有相同的签名和/或返回值类型(例如,如果您正在编写一个连接器来从一个数据库检索非标准化数据,对其进行标准化,然后将其插入到第二个数据库中)标准化数据库,并且您有一堆类似的方法,例如 15 个 read_and_normalize_table_X 方法),能够即时检索(并运行)它们可能会更有用。