将对象传递给装饰器

Passing an object to a decorator

我正在尝试创建一个 Python 文件,其中包含我需要在程序的其余部分使用的所有装饰器。这些装饰器存储在一个 class 中,我称之为 Decorators。然后我尝试添加一个装饰器来检查装饰函数的参数是否与传递给装饰器本身的参数类型匹配(我从站点 https://www.python.org/dev/peps/pep-0318/#examples 的示例 4 中使用了这种装饰器,但我改变了它有点更适合我的编码风格)。语法是这样的:

class Decorators(object):

    """ Decorators class: contain all the decorators """

    @classmethod
    def argument_consistency(cls, *function_arguments_type):

        """ check the consistency of argument and their types of the decorated function """

        def check_arguments(function):

            """ check if the number of passed arguments is different from the number of accepted arguments """

            # check if the number of passed arguments is different from the number of accepted arguments
            if not len(function_arguments_type) == function.__code__.co_argcount:
                raise Exception("the number of passed argument is different from the number of the accepted arguments")

            def inner_function(*args, **kwds):

                """ check if the type of the passed arguments match with the requested ones """

                # iterate through the list of couples (argument, argument's type) and check for their match
                for (arguments, argument_types) in zip(args, function_arguments_type):

                    # remember that: {arguments} is the n-th argument passed to the function, while
                    # the {argument_types} is the n-th argument types. {args} is the entire list of arguments
                    # passed to the function; {function_arguments_type} is the entire list of types. So zip
                    # returns an iterator of tuples of element of {args} and {function_arguments_type} paired
                    # together, for example zip((1, 2, 3, 4), (a, b, c, d)) = ((1, a), (2, b), (3, c), (4, d))
                   
                    # check if the n-th argument type match with the one requested
                    if not type(arguments) == argument_types:
                        raise Exception(f"The argument {arguments} doesn't match the type, "
                                        f"which must be {argument_types}")

                # returning the passed function using the passed arguments
                return function(*args, **kwds)

            # changing the name of the inner_function to the {function}'s name
            inner_function.__name__ = function.__name__

            # return the inner function
            return inner_function

        # return the check_argument function
        return check_arguments

为了测试之前的装饰器,我创建了简单的 class A 函数 a:

class A():

    def __init__(self):
        pass

    @Decorators.argument_consistency(str, str)
    def a(self, str1, str2):
        print(f"{str1} AND {str2}")

a = A()
a.a("ciao", "ciao2")

显然,当我装饰函数 a 时出现错误(由 argument_consistency 装饰器本身引发)。这是因为列表参数类型的长度与传递参数列表的长度不同。出现错误是因为我没有输入 self 参数。理解这个错误,我尝试将 self 传递给装饰器,但我得到一个错误: NameError: name 'self' is not defined (即使我传递 type(self) 也会发生这种情况);然后我尝试传递 class A 本身,但我仍然遇到同样的错误。所以我试图通过在装饰器 for 循环和 if not type(arguments) == argument_types:

之间添加一行来解决这个问题
if not (args.index(arguments) == 0 and argument_types is None):
    
    # check if the n-th argument type match with the one requested
    if not type(arguments) == argument_types:

        # the rest of the code
        pass

这行检查传递给函数装饰器的第一个参数是否是None,那么这意味着函数的第一个参数是self,所以函数不会继续检查如果 None 等于 self 参数的类型(显然不是)。这种方式非常繁琐,与优雅背道而驰。因此我想知道是否有办法避免此修复并直接将 self 类型传递给装饰器。

您可以为 object/class 方法

的自变量创建存根 class
class selftype:
    pass

并将其传递给装饰器

@Decorators.argument_consistency(selftype, str, str)
def a(self, str1, str2):
    print(f"{str1} AND {str2}")

然后检查 inner_function 如果装饰器中的第一个类型是你的存根类型:

def inner_function(*args, **kwds):
    for (argument, argument_type, i) in zip(args, function_arguments_type, range(0, len(args))):
        if argument_type == selftype and i == 0:
            pass
        # check if the n-th argument type match with the one requested
        elif not type(argument) == argument_type:
            raise Exception(f"The argument {argument} doesn't match the type, "
                            f"which must be {argument_type}")

    # returning the passed function using the passed arguments
    return function(*args, **kwds)

不是很优雅,但这对object/class方法和函数都有效

class A():

    def __init__(self):
        pass

    @Decorators.argument_consistency(selftype, str, str)
    def a(self, str1, str2):
        print(f"{str1} AND {str2}")

a = A()
a.a("ciao", "ciao2")

@Decorators.argument_consistency(str)
def b(str1):
    print(f"{str1}")

b("a")

此外,如果你想与@classmethod@staticmethod配对使用你的装饰器,请确保先应用你的装饰器,否则将无法访问function.__code__属性。

我非常喜欢@go2nirvana 在评论中提出的解决方案,但不幸的是它对我不起作用。 inspect.ismethod(function) returns False 内部装饰器函数调用,我知道为什么。