TypeError:'str'对象在Ipython控制台中不可调用,但不能在外部调用

时间:2015-11-17 09:45:43

标签: python python-2.7 numpy

执行:

import numpy as np
x = np.array([ 0.815,  0.02 , -0.053])
" ".join(map(str, x))
IPython控制台中的

给出了错误:

TypeError: 'str' object is not callable

但是,当我在外部系统终端上执行它时,它运行正常!

2 个答案:

答案 0 :(得分:3)

您已将strmap设为字符串:

>>> map = 'some string'
>>> x = [1, 2]
>>> " ".join(map(str, x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable
>>> del map
>>> str = 'some string'
>>> " ".join(map(str, x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

错误消息告诉您尝试将(...)调用语法应用于的对象类型;它不是调用表达式中使用的名称。

在交互式会话中,只需删除名称,内置类型将重新出现:

>>> str = 'some string'
>>> str
'some string'
>>> del str
>>> str
<type 'str'>

答案 1 :(得分:1)

确保您没有覆盖str内置功能:

In [1]: import numpy as np

In [2]: x = np.array([ 0.815,  0.02 , -0.053])

In [3]: " ".join(map(str, x))
Out[3]: '0.815 0.02 -0.053'
In [4]: str = ''  # <--- overwrite `str`

In [5]: " ".join(map(str, x))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-69d3e83718e2> in <module>()
----> 1 " ".join(map(str, x))

TypeError: 'str' object is not callable