2015-11-13 87 views
0

我一直試圖使用str()函數將整數轉換爲Spyder中的字符串(python 2.7)。我TypeError: 'str' object is not callablePython的Str對象不能在Spyder中調用

例如每一次,我寫了這個簡單的代碼來測試它,我得到了同樣的錯誤:

x = 5 
print str(x) 

有人可以幫我在這

+4

您可能創建了另一個名爲'str'的​​變量。如果您在全新的解釋器會話中運行該代碼,則不應該出現該錯誤。 – BrenBarn

回答

2

已覆蓋內置在str的代碼中。

>>> str = 'foo' # overwriting the builtin `str` 
>>> x = 5 
>>> print str(x) # equivalent to 'foo'(x) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: 'str' object is not callable 
+0

我檢查了我的代碼,我沒有在任何地方找到str!有沒有另一種方法將int轉換爲String? – Nasser

+0

@Nasser:再次檢查您的代碼。你是否導入了任何模塊,比如'from any import *'?如果是這樣,請檢查這些模塊。另外,在'print str(x)''print repr(str)'之前'。這應該有助於你弄清楚事情的真相。 –