出错代码:
>>> x = 1.99
>>>
>>> str(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
原因:之前在同一个python解释器中定义了一个叫str
的变量…于是这个变量覆盖了原有的str()函数,因此解释器会认为str不是一个函数,不可调用。
>>> global str
>>>
# some times ago, i assign string 'hello' to str variable, so python interpreter think str is a string variable.
>>> str = 'hello'
>>>
......
......
......
>>> x= 1.99
# so when i invoke str() function, the interpreter think str is a string variable then error happened.
>>> str(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
解决办法:
del str # 删除之前定义的str变量
总结:
永远不要把关键字、内置函数名等作为变量名!