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)
我尝试使用修饰符检查输入参数类型。但它不起作用。在
错误:
if not all(map(lambda x: isinstance(x, type), args)): E TypeError: isinstance() arg 2 must be a type or tuple of types
您的装饰函数签名颠倒了:
wrapper
应采用原始包装的函数,args_typecheck
应采用要检查的类型:相关问题 更多 >
编程相关推荐