我在一台装有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'同时,在我的Mac上运行Python3.4.3的时候,同样的代码也运行得很流畅。这可能是由于Python版本之间的差异,还是我遗漏了什么?
发布于 2015-08-18 00:54:46
list.clear是在Python3.3中添加的。
引用文档中的部分:
3.3版中的新特性:
clear()和copy()方法。
s.clear()从s中删除所有项目(与del s[:]相同)
有关清除列表的相关讨论和替代方法,请参阅issue #10516。总而言之,它与del l[:]和l[:] = []相同。
https://stackoverflow.com/questions/32055768
复制相似问题