Python 如何实现具有两个arfument函数的decorator

Python 如何实现具有两个arfument函数的decorator,python,Python,我以前没有使用过decorator,但是现在我需要实现一些可重复的测试来比较一些数据列表,并且我希望为此使用更少的代码。我不希望有一个函数来比较两个数据集的数据列表 def compare(a,b): for j, c in zip(a, b): try: assert j == c except AssertionError: raise AssertionError(f"\n{a} arr1: {j}\

我以前没有使用过decorator,但是现在我需要实现一些可重复的测试来比较一些数据列表,并且我希望为此使用更少的代码。我不希望有一个函数来比较两个数据集的数据列表

 def compare(a,b):
    for j, c in zip(a, b):
        try:
            assert j == c
        except AssertionError:
            raise AssertionError(f"\n{a} arr1: {j}\n{b} arr2:  {c}")
这是两个列表:

arr1 = [arg1, arg2, arg3, arg4]
arr2 = [arg1, arg2, arg3, arg4]
@decor
def lists():
    arr1 = [arg1, arg2, arg3, arg4]
    arr2 = [arg1, arg2, arg3, arg4]
    return arr1, arr2
我正在创建decorator:

def decor(func):
    def comp(a,b):
        for j, c in zip(a, b):
            try:
                assert j == c
            except AssertionError:
                raise AssertionError(f"\n{a}: {j}\n{b}:  {c}")
            func()
    return comp
def decor(func):
    def wrapper(*a, **kw):
        a, b = func(*a, **kw)
        compare(a,b)
        return a, b
    return wrapper
然后用两个列表定义方法:

arr1 = [arg1, arg2, arg3, arg4]
arr2 = [arg1, arg2, arg3, arg4]
@decor
def lists():
    arr1 = [arg1, arg2, arg3, arg4]
    arr2 = [arg1, arg2, arg3, arg4]
    return arr1, arr2
但我遇到了一个错误,不知道如何设置预期参数:


TypeError:comp()缺少两个必需的位置参数:“a”和“b”

如果您知道您的函数将返回两个列表,并且必须使用装饰程序来执行此操作,我建议暂时将
比较(a,b)
函数分开,以保持简单。然后在装饰器中:

def decor(func):
    def comp(a,b):
        for j, c in zip(a, b):
            try:
                assert j == c
            except AssertionError:
                raise AssertionError(f"\n{a}: {j}\n{b}:  {c}")
            func()
    return comp
def decor(func):
    def wrapper(*a, **kw):
        a, b = func(*a, **kw)
        compare(a,b)
        return a, b
    return wrapper

顺便说一下-。您得到了错误,因为装饰器的内部函数需要2个参数,但是
list
没有参数