Python3 AttributeError: 'list' 对象没有属性 'clear'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32055768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python3 AttributeError: 'list' object has no attribute 'clear'
提问by oranJess
I am working on a Linux machine with Python version 3.2.3.
Whenever I try to do list.clear()
I get an exception
我正在使用 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'
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?
同时在我的 Mac 上使用 Python 3.4.3 运行相同的代码。可能是由于 Python 版本之间的差异还是我遗漏了什么?
采纳答案by vaultah
list.clear
was added in Python 3.3.
list.clear
在 Python 3.3 中添加。
Citing the Mutable Sequence Typessection in the documentation:
引用文档中的可变序列类型部分:
New in version 3.3:
clear()
andcopy()
methods.
s.clear()
removes all items froms
(same asdel s[:]
)
3.3 版中的新功能:
clear()
和copy()
方法。
s.clear()
从s
(与 相同del s[:]
)中删除所有项目
See the issue #10516for the relevant discussion and alternative ways of clearing lists. In summary, it is the same as del l[:]
and l[:] = []
.
有关清除列表的相关讨论和替代方法,请参阅问题 #10516。总之,它与del l[:]
和相同l[:] = []
。