Python3 AttributeError:“列表"对象没有属性“清除" [英] Python3 AttributeError: 'list' object has no attribute 'clear'
问题描述
我正在使用Python版本3.2.3的Linux机器上工作.
每当我尝试执行list.clear()
时,都会出现异常
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'
在Mac上同时使用Python 3.4.3的情况下,相同的代码可以平稳运行. 可能是由于Python版本之间的差异所致,还是我缺少了某些东西?
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
已在Python 3.3中添加.
list.clear
was added in Python 3.3.
引用 可变序列类型 部分:
3.3版中的新功能:
clear()
和copy()
方法.
s.clear()
删除s
中的所有项目(与del s[:]
相同)
s.clear()
removes all items from s
(same as del s[:]
)
有关相关讨论和清除列表的替代方法,请参见 issue#10516 .总之,它与del l[:]
和l[:] = []
相同.
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:“列表"对象没有属性“清除"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!