如何创建和使用从列表继承的新Python类

时间:2014-12-05 10:44:09

标签: python list class inheritance

我正在尝试创建一个继承自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类的初始化方法吗?

0 个答案:

没有答案