简体   繁体   English

我可以在不更改方法/函数签名的情况下在python中使用方面吗?

[英]Can I use aspects in python without changing a method / function's signature?

I've been using python-aspectlib to weave an aspect to certain methods - unfortunately this changes the methods signature to Argspec(args=[], varargs='args', keywords='kwargs', default=None) , which creates problems when working with libraries that depend on inspect returning the proper signature(s). 我一直在使用python-aspectlib为某些方法编织一个方面-不幸的是,这将方法签名更改为Argspec(args=[], varargs='args', keywords='kwargs', default=None) ,这会产生问题当使用依赖inspect库时,返回正确的签名。

Is there a way to use python-aspectlib without changing a method's signature? 有没有一种方法可以使用python-aspectlib而不更改方法的签名? If not, are there other python aspect libraries that can do this? 如果没有,还有其他的Python方面库可以做到这一点吗?

I've looked at the decorator module, which explicitly mentions the problem of changing a method signature: http://micheles.googlecode.com/hg/decorator/documentation.html#statement-of-the-problem , but I was hoping to find a solution where I don't need to modify the methods I want to weave (since they are part of a third party library). 我查看了装饰器模块,该模块明确提到了更改方法签名的问题: http : //micheles.googlecode.com/hg/decorator/documentation.html#statement-of-the-problem ,但我希望找到不需要修改我要编织的方法的解决方案(因为它们是第三方库的一部分)。

I'm using python 2.7.6 我正在使用python 2.7.6

I've managed to 'fix' this for my specific use case with the following piece of code: 我已经通过以下代码针对特定用例成功解决了此问题:

from decorator import decorator
from Module1 import Class1
from Module2 import Class2

def _my_decorator(func, *args, **kwargs):
    #add decorator code here
    return func(*args, **kwargs)


def my_decorator(f):
    return decorator(_my_decorator, f)

methods_to_decorate = [
    'Class1.method1',
    'Class2.method2',
    ]

for method in methods_to_decorate:
    exec_str = method + '= my_decorator('+method+'.im_func)'
    exec(exec_str)

This probably doesn't handle all of the issues mentioned in the How you implement your Python decorator is wrong blog posts, but it full fills the criteria most important to me: correct method signatures. 这可能无法解决“ 如何实现Python装饰器是错误的”博客文章中提到的所有问题,但它完全满足了我最重要的条件:正确的方法签名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 装饰器如何在不改变签名的情况下将变量传递给函数? - How can a decorator pass variables into a function without changing its signature? 在Python中,如何在第二个函数中使用函数的return语句而不使用全局变量? - In Python, how can I use a function's return statement within a second function without using global variables? 如何在不结束Python函数的情况下返回变化的变量? - How can I return a changing variable without ending the function in Python? 如何在不更改参数名称的情况下装饰Python函数? - How can I decorate a Python function without changing the names of the arguments? 我可以使用 inspect.signature 将装饰器的 (*args, **kwargs) 解释为装饰函数的变量吗? - Can I use inspect.signature to interpret a decorator's (*args, **kwargs) as the decorated function's variables would be? 如何复制 python 函数的签名并定义新的 function? - How can I copy a python function's signature and define a new function? 难以在整个Celery上更改Python方法签名 - Difficulty changing a Python method signature across Celery 我可以使用 Class 或 Method 作为 Python 中的枚举类的值吗? - can i use Class or Method as Enum class's value in Python? 如何在python中将函数传递给类的方法? - How can I pass a function to a class's method in python? Python 子 class 中方法返回的覆盖类型提示,没有重新定义方法签名 - Python overriding type hint on a method's return in child class, without redefining method signature
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM