Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/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中的装饰器。如何检查args类型_Python_Decorator - Fatal编程技术网

Python中的装饰器。如何检查args类型

Python中的装饰器。如何检查args类型,python,decorator,Python,Decorator,我尝试使用装饰器检查输入参数的类型。但它不起作用 错误: 如果不是全部(映射(lambda x:isinstance(x,type,args)):E类型错误: isinstance()arg 2必须是类型或类型的元组 您的decorator函数签名是反向的:wrapper应采用原始的包装函数,args\u typecheck应采用要检查的类型: def args_typecheck(func): def wrapper(type): def inner(*args

我尝试使用装饰器检查输入参数的类型。但它不起作用

错误:

如果不是全部(映射(lambda x:isinstance(x,type,args)):E类型错误: isinstance()arg 2必须是类型或类型的元组


您的decorator函数签名是反向的:
wrapper
应采用原始的包装函数,
args\u typecheck
应采用要检查的类型:

def args_typecheck(func):    
    def wrapper(type):
        def inner(*args):
            if not all(map(lambda x: isinstance(x, type), args)):
                raise TypeError
            return func(*args)
        return inner
    return wrapper

@args_typecheck(str)
def seq(*args):
    return reduce(operator.eq, args)
def args_typecheck(type):
   def wrapper(func):
      def inner(*args):
        if not all(map(lambda x: isinstance(x, type), args)):
            raise TypeError
        return func(*args)
      return inner
   return wrapper

@args_typecheck(str)
def seq(*args):
  return reduce(operator.eq, args)