鱼C论坛

 找回密码
 立即注册
查看: 4067|回复: 6

python47讲课后作业求解答

[复制链接]
发表于 2021-4-3 20:31:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
新手求教,为什么__init__方法里去掉super.__init__会报错IndexError?

  1. class CountList(list):
  2.     def __init__(self, *args):
  3.         super().__init__(args)
  4.         self.count = []
  5.         for i in args:
  6.             self.count.append(0)

  7.     def __len__(self):
  8.         return len(self.count)

  9.     def __getitem__(self, key):
  10.         self.count[key] += 1
  11.         return super().__getitem__(key)

  12.     def __setitem__(self, key, value):
  13.         self.count[key] += 1
  14.         super().__setitem__(key, value)

  15.     def __delitem__(self, key):
  16.         del self.count[key]
  17.         super().__delitem__(key)

  18.     def counter(self, key):
  19.         return self.count[key]

  20.     def append(self, value):
  21.         self.count.append(0)
  22.         super().append(value)

  23.     def pop(self, key=-1):
  24.         del self.count[key]
  25.         return super().pop(key)

  26.     def remove(self, value):
  27.         key = super().index(value)
  28.         del self.count[key]
  29.         super().remove(value)

  30.     def insert(self, key, value):
  31.         self.count.insert(key, 0)
  32.         super().insert(key, value)

  33.     def clear(self):
  34.         self.count.clear()
  35.         super().clear()

  36.     def reverse(self):
  37.         self.count.reverse()
  38.         super().reverse()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-21 19:10:23 | 显示全部楼层

回帖奖励 +3 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-21 22:21:23 | 显示全部楼层

回帖奖励 +3 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-22 00:17:24 | 显示全部楼层

回帖奖励 +3 鱼币

看看有大神解决了吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-22 02:11:34 | 显示全部楼层

回帖奖励 +3 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-7-22 13:15:01 | 显示全部楼层

回帖奖励 +3 鱼币

怀疑你们在水贴
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-24 18:54:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 06:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表