Python3 属性错误 : 'list' object has no attribute 'clear'

标签 python list python-3.x attributeerror python-3.2

我正在使用 Python 版本 3.2.3 的 Linux 机器上工作。 每当我尝试执行 list.clear() 时,我都会遇到异常

>>> l = [1, 2, 3, 4, 5, 6, 7]
>>> l.clear()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'clear'

同时在装有 Python 3.4.3 的 Mac 上,相同的代码运行流畅。 可能是由于 Python 版本之间的差异还是我遗漏了什么?

最佳答案

list.clear 是在 Python 3.3 中添加的。

引用 Mutable Sequence Types文档中的部分:

New in version 3.3: clear() and copy() methods.

s.clear() removes all items from s (same as del s[:])

参见 issue #10516用于相关讨论和清算列表的替代方法。综上所述,与del l[:]l[:] = []是一样的。

关于Python3 属性错误 : 'list' object has no attribute 'clear' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32055768/

相关文章:

java - 组合自定义类中的项目

python - 如何在运行时在 Python 中从 stub 文件 (.pyi) 获取类型注释?

python - 计算 3-D 旋度的最快算法

python - 使用 python 从 CSV 文件创建具有以下结构的 json 文件

python - CrawlerRunner 不等待?

python - python 中的列表是不可变的吗?它们是在函数中传递值吗?

python - Pandas 数据框行操作

Java 8 流逆序

python - 如何打印一个字符串以在打印输出中包含多行

python - Python 3 与 Python 2 中的映射