Python3 AttributeError:'list'对象没有属性'clear'

ora*_*ess 12 python list attributeerror python-3.x 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'
Run Code Online (Sandbox Code Playgroud)

同时在我的Mac上使用Python 3.4.3,相同的代码运行顺畅.它可能是由于Python版本之间的差异还是我缺少的东西?

vau*_*tah 22

list.clear 在Python 3.3中添加.

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

版本3.3中的新功能:clear()和copy()方法.

s.clear()删除所有项目s(相同del s[:])

有关讨论和清除列表的替代方法,请参阅问题#10516.总之,它与del l[:]和相同l[:] = [].

  • 只需添加即可使用`del l [:]`. (5认同)