2016-07-26 59 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'