我正在驗證某一段數據,在這種情況下,由於它需要5(一個int)Python類型()函數問題,str不可調用
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
我在做什麼錯在這裏?我想打印傳遞的值的類型,並且我的自定義驗證失敗。
' 'five''是一個字符串,所以是'' 5''。 – 2013-04-25 23:12:51
看起來像你在某處定義了一個變量名「type」。 – 2013-04-25 23:14:04
有點偏題,如果你正在尋找一個Python數據驗證庫,那麼你可以看看[voluptuous](https://github.com/alecthomas/voluptuous),它很小,有一個設計良好的界面並且易於擴展。 – miku 2013-04-25 23:16:19