python如何删除对象属性,Python3 AttributeError:“列表”对象没有属性“清除”

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 "", line 1, in

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值