鱼C论坛

 找回密码
 立即注册
查看: 2843|回复: 4

__init__()方法的一个小问题

[复制链接]
发表于 2016-2-26 14:54:29 | 显示全部楼层 |阅读模式

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

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

x
  1. #定制一个列表,记录列表中每个元素被访问的次数。要求列表 的功更加全面 47课后作业
  2. class CountList(list):
  3.     def __init__(self,*args):
  4.         super().__init__(args)
  5.         self.count = []
  6.         for i in args:
  7.             self.count.append(0)

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

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

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

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

  19.     def counter(self,key):
  20.         return self.count[key]
  21.     def append(self,value):
  22.         self.count.append(0)
  23.         super().append(value)
  24.     def pop(self,key=-1):
  25.         del self.count[key]
  26.         return super().pop(key)
  27.     def remove(self,value):
  28.         key = super().index(value)
  29.         del self.count[key]
  30.         return super().remove(value)
  31.     def insert(self,key,value):
  32.         self.count.insert(key,0)
  33.         super().insert(key,value)
  34.     def clear(self):
  35.         self.count.clear()
  36.         super().clear()
  37.     def reverse(self):
  38.         self.count.reverse()
  39.         super().reverse()
复制代码

请问开头写super().__init__(args)的具体作用是什么?我不太理解里面的工作原理?谢谢
我试了下,如果不写这句话,则为空列表
>>> c = CountList(1,2,3)
>>> c
[]

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-26 21:15:29 | 显示全部楼层
继承父类list中的__init__,这样输入的参数才会成为列表
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-26 21:29:17 | 显示全部楼层
Lnan95 发表于 2016-2-26 21:15
继承父类list中的__init__,这样输入的参数才会成为列表

恩恩 好的 谢谢,第一次接触到这样写,理解的不深入。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-2-26 21:39:28 | 显示全部楼层
吕浩 发表于 2016-2-26 21:29
恩恩 好的 谢谢,第一次接触到这样写,理解的不深入。。。

不急,小甲鱼后面的教程有介绍哈~
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-2-26 21:58:59 | 显示全部楼层
Lnan95 发表于 2016-2-26 21:39
不急,小甲鱼后面的教程有介绍哈~

恩恩 好的  ,努力艰难进行中。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-19 20:40

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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