python - 为什么 int 列表在 python 3.7 及更高版本中没有得到转换,但在 python 2.7 上有效?

标签 python python-3.x python-2.7

<分区>

Python 2.7 如下

>>> l1 =[1,2,3]
>>> for x in l1:
  str(x)


'1'
'2'
'3'
>>> 

为什么在 python 3.7 中,int 到 str 不能像上面在 2.7 中看到的那样工作?

Python 3.7 如下

>>> l1[1, 2, 3, 4]
>>> for x in l1:print(str(x))
Traceback (mostrecentcalllast):File <pyshell#593>", line 2, in <module>
     print(str(x))
     TypeError: 'str' object is not callable

最佳答案

这可能是因为您在早些时候定义了一个变量 str:

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

您应该避免为变量使用与内置函数相同的名称(如 str()),否则可能会发生上述情况。

关于python - 为什么 int 列表在 python 3.7 及更高版本中没有得到转换,但在 python 2.7 上有效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55783040/

相关文章:

python - 在 django 中导出 CSV 文件时出现 405

python - Django FilterSet AND(?)条件

javascript - 矩阵中单词的出现顺序为从左到右、向下或对 Angular 线向左(向上或向下)

python - Cookiecutter Django 与 PostgreSQL 不兼容

python - Pandas DataFrame 相等 - 索引编号

python - Python 中的 For 循环 x 来自文件的次数

python - 如何使用 Numba 释放 Python 中线程的 GIL?

python-3.x - 列名和最大值索引

python-3.x - 在标题中对齐具有不同形状和 q_offset 值的 nifti 文件

python正则表达式查找外野手的名字