"str"对象不能使用枚举调用



我正在尝试执行以下操作:

for index, image_properties in enumerate(list_of_properties):
    index = str(index)

但我不断得到

TypeError at /
'str' object is not callable

这里出了什么问题?

正如评论者所提到的,您必须在某处定义str并且它会覆盖str内置函数。

在 Python 中,你可以轻松地像这样"重新绑定"符号。例如,请参阅此会话:

>>> str(2)
'2'
>>> def str(x): return x + 1
... 
>>> str(2)
3
>>> str = 1
>>> str(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

此外,TypeError的文本表明str在更早的某个地方被定义为字符串对象。