我正在验证某段数据,在这种情况下,字符串g 'five'应该失败某段验证,因为它需要是5 (int)
print ">>>>", value
bad_type = type(value).__name__
raise TypeError("expected numeric type for {0} but got '{1}'".format(column,bad_type))
打印:
.>>>> five
...
bad_type = type(value).__name__
TypeError: 'str' object is not callable
但是我可以从命令行做到这一点:
python -c "print type('five').__name__"
打印
str
我在这里做错了什么?我想打印传递的值的类型,并且我的自定义验证失败。
您确定您没有在某个地方覆盖type
吗?
同样,这不是python的类型检查方式——而是使用:
try:
my_int = int(value)
except (ValueError, TypeError) as e:
# raise something