鱼C论坛

 找回密码
 立即注册
查看: 764|回复: 2

047讲动动手的问题

[复制链接]
发表于 2020-2-26 09:49:23 | 显示全部楼层 |阅读模式

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

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

x
1.想问一下大家,这个继承的列表是list,是如何实现的?我还不太懂
2.增加了这么多功能,该如何调用呢,发现对这个代码里面的函数实现有点困难,能不能给我举个例子
3.最后的是搞了这么多的代码实现的功能又是啥













  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
回复

使用道具 举报

发表于 2020-2-26 09:56:11 | 显示全部楼层
1. 就是简单地添加一个 (list)

class CountList(list):

2. 自己试一下就行了,和列表的方法基本相同。

3. 实现一个 “计数列表”
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-26 10:01:13 | 显示全部楼层
他会看你的电脑文件名,我不知道它有多大的用处,我只知道它在游戏中能当一些数量关系式
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-30 18:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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