Python3 AttributeError:“list”对象没有属性“clear”

时间:2022-02-27 10:47:15

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版本之间的差异,还是我漏掉了什么?

1 个解决方案

#1


14  

list.clear was added in Python 3.3.

列表。在Python 3.3中添加了clear。

Citing the Mutable Sequence Types section in the documentation:

引用文档中的可变序列类型部分:

New in version 3.3: clear() and copy() methods.

新版本3.3:clear()和copy()方法。

s.clear() removes all items from s (same as del s[:])

s.clear()从s中删除所有项目(与del 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[:] = [].

有关清单结算的讨论和其他方式,请参见问题#10516。综上所述,它与del l[:]和l[:] =[]相同。

#1


14  

list.clear was added in Python 3.3.

列表。在Python 3.3中添加了clear。

Citing the Mutable Sequence Types section in the documentation:

引用文档中的可变序列类型部分:

New in version 3.3: clear() and copy() methods.

新版本3.3:clear()和copy()方法。

s.clear() removes all items from s (same as del s[:])

s.clear()从s中删除所有项目(与del 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[:] = [].

有关清单结算的讨论和其他方式,请参见问题#10516。综上所述,它与del l[:]和l[:] =[]相同。