Python中的装饰器。如何检查args类型
我尝试使用装饰器检查输入参数的类型。但它不起作用 错误: 如果不是全部(映射(lambda x:isinstance(x,type,args)):E类型错误: isinstance()arg 2必须是类型或类型的元组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
您的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)