Python3 AttributeError: 'list' 对象没有属性 'clear' [英] Python3 AttributeError: 'list' object has no attribute 'clear'

查看:44
本文介绍了Python3 AttributeError: 'list' 对象没有属性 'clear'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 版本 3.2.3 的 Linux 机器上工作.每当我尝试执行 list.clear() 时,我都会遇到异常

<预><代码>>>>l = [1, 2, 3, 4, 5, 6, 7]>>>l.clear()回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中AttributeError: 'list' 对象没有属性 'clear'

同时在使用 Python 3.4.3 的 Mac 上,相同的代码运行流畅.可能是由于 Python 版本之间的差异,还是我遗漏了什么?

解决方案

list.clear 已在 Python 3.3 中添加.

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

<块引用>

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

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

请参阅 issue #10516 以了解相关讨论和清除列表的替代方法.综上所述,与del l[:]l[:] = []是一样的.

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?

解决方案

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[:] = [].

这篇关于Python3 AttributeError: 'list' 对象没有属性 'clear'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆