python
class CountList(list):def __init__(self, *args):
super().__init__(args)
self.count = []
for i in args:
self.count.append(0)
def __len__(self):
return len(self.count)
def __getitem__(self, key):
self.count += 1
return super().__getitem__(key)
def __setitem__(self, key, value):
self.count += 1
super().__setitem__(key, value)
def __delitem__(self, key):
del self.count
super().__delitem__(key)
def counter(self, key):
return self.count
def append(self, value):
self.count.append(0)
super().append(value)
def pop(self, key=-1):
del self.count
return super().pop(key)
def remove(self, value):
key = super().index(value)
del self.count
super().remove(value)
def insert(self, key, value):
self.count.insert(key, 0)
super().insert(key, value)
def clear(self):
self.count.clear()
super().clear()
def reverse(self):
self.count.reverse()
super().reverse()
课后作业047,class CountList(list):
def __init__(self, *args):
super().__init__(args),这个super()这里什么意思啊,没看明白,不像视频里面一样用列表推导式什么的嘛
super()表示不重写父类的__init__方法,同时享用父类的__init__方法 另外发代码请使用<>包裹起来,这样查看不方便
页:
[1]