2016-07-26 61 views
0

我尝试了网站上的例子,给出了结果'1'的类型转换,我得到了下面的结果(在Python 2.7中)。我不明白发生了什么事。什么导致了错误?关于Python类型转换的错误

str(1) 
Traceback (most recent call last): 
    File "interactive input", line 1, in module 
TypeError: 'str' object is not callable 
+0

无法复制此 –

+2

您可能在本次会议的早些时候做了'str = '。 – user2357112

回答

2

您可能已使用str作为变量。因此,你正在得到这个错误。

>>> str = '' 
>>> str(1) 

Traceback (most recent call last): 
    File "<pyshell#3>", line 1, in <module> 
    str(1) 
TypeError: 'str' object is not callable 

str是一个内置类和你只是用它作为一个用户定义的变量覆盖它。

您可以重新启动您的shell或只需使用del str删除变量定义(如@Ken Y-N所示)。

>>> del str 
>>> str(1) 
'1'