我正在尝试创建一个继承自list
的简单类,但我不确定我是否正确使用它。我的课程定义如下:
class pairs(list):
def __init__(
self,
*args
):
# list initialisation
super().__init__(self, *args)
def update(updatePairs):
for updatePair in range(len(updatePairs)):
found_match = False
for pair in range(len(self)):
# If there is a matching 'key' in a pair of the existing list of
# pairs, record that a match was found and update the existing
# pair.
if updatePairs[updatePair][0] == self[pair][0]:
found_match = True
self[pair][1] = updatePairs[updatePair][1]
# If a matching 'key' is not found, there is no pair to update.
# Append the update pair to the existing list of pairs.
if not found_match:
self.append(updatePairs[updatePair])
当我尝试实例化它时,我遇到了一点困难,因为list
类的初始化方法期望至少有一个参数:
>>> a = pairs.pairs()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pairs.py", line 7, in __init__
# list initialisation
TypeError: super() takes at least 1 argument (0 given)
我在这里做错了什么?我是否错误地实例化了该对象?我应该以不同的方式使用list
类的初始化方法吗?