使用 Python 2.5.2(r252:60911,2008 年 9 月 30 日,15:42:03) 我尝试使用
type(x)
并在传入此字符串 '2014-02-06 00:00:00' 时收到以下错误
TypeError: 'str' 对象不可调用
为什么不返回str?
请您参考如下方法:
您可能通过执行以下操作用字符串隐藏了 python 函数 type:
>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> type(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
您可以通过不使用也是 python 内置函数的变量名来解决此问题。
如果您绝对不能将您的变量重命名为 type 以外的名称(_type 或 type_ 都可以), 您可以使用 __builtin__ 模块访问内置函数
>>> type = 'some string'
>>> # Lots of code
>>> x = 'other string'
>>> import __builtin__
>>> __builtin__.type(x)
<type 'str'>
不过,任何阅读您的代码的人都会讨厌您。
