我有一个装饰器检查int
类型的函数参数。装饰函数返回无
def check_type_int(old_function):
def new_function(arg):
if not isinstance(arg, int):
print 'Bad Type' # raise TypeError('Bad Type')
else:
old_function(arg)
return new_function
当我运行一个装饰作用,它返回None
代替int
值。
@check_type_int
def times2(num):
return num*2
times2('Not A Number') # prints "Bad Type"
print times2(2) # prints "None"
最后一行应打印4
。有人可以发现我的错误吗?谢谢。
你为什么要明确检查类型也是这样吗?为什么'打印'而不是提出错误,如果类型是无效的? – jonrsharpe