Python3 AttributeError: 'list' 对象没有属性 'clear' [英] Python3 AttributeError: 'list' object has no attribute 'clear'
问题描述
我正在使用 Python 版本 3.2.3 的 Linux 机器上工作.每当我尝试执行 list.clear()
时,我都会遇到异常
同时在使用 Python 3.4.3 的 Mac 上,相同的代码运行流畅.可能是由于 Python 版本之间的差异,还是我遗漏了什么?
list.clear
已在 Python 3.3 中添加.
引用 可变序列类型 文档中的部分:
<块引用>3.3 版的新功能:clear()
和 copy()
方法.
s.clear()
从 s
中删除所有项目(与 del s[:]
相同)
请参阅 issue #10516 以了解相关讨论和清除列表的替代方法.综上所述,与del l[:]
和l[:] = []
是一样的.
I am working on a Linux machine with Python version 3.2.3.
Whenever I try to do list.clear()
I get an exception
>>> 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'
At the same time on my Mac with Python 3.4.3 the same code runs smoothly. Can it be due to the difference between Python versions or is there something I'm missing?
list.clear
was added in Python 3.3.
Citing the Mutable Sequence Types section in the documentation:
New in version 3.3:
clear()
andcopy()
methods.
s.clear()
removes all items froms
(same asdel s[:]
)
See the issue #10516 for the relevant discussion and alternative ways of clearing lists. In summary, it is the same as del l[:]
and l[:] = []
.
这篇关于Python3 AttributeError: 'list' 对象没有属性 'clear'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!