Python3 AttributeError: 'list' object has no attribute 'clear'

和自甴很熟 提交于 2019-12-18 20:16:02

问题


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'

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?


回答1:


list.clear was added in Python 3.3.

Citing the Mutable Sequence Types section in the documentation:

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

s.clear() removes all items from s (same as 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[:] = [].



来源:https://stackoverflow.com/questions/32055768/python3-attributeerror-list-object-has-no-attribute-clear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!