不明白为什么在尝试调用继承的方法时会发生此TypeError

2024-09-30 20:35:04 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,我使用的是python3.6。你知道吗

我正在尝试import并在我的项目中使用我自己的.py文件。我import我的LinkedList.py文件并创建一个Mylist类,它扩展了导入文件的类。你知道吗

当我尝试构造Mylist类的实例时,它涉及到创建我的inheritedLinkedList派生类的实例,我得到以下错误:

Traceback (most recent call last):
  File "*/PycharmProjects/Veri Yapilari/lists.py", line 65, in <module>
    test = Mylist()
  File "*/PycharmProjects/Veri Yapilari/lists.py", line 38, in __init__
    self.linkedlist = inheritedLinkedList()
  File "*/PycharmProjects/Veri Yapilari/lists.py", line 8, in __init__
    super.__init__()
TypeError: descriptor '__init__' of 'super' object needs an argument

以下是出现问题的代码部分:

test = Mylist()
test.insertFirstM(incomingDataM=4)  # <- Causes a TypeError.

以下是完整的主脚本:

import LinkedList as myLinkedList


class inheritedLinkedList(myLinkedList.DoublyLinkedList):
    def __init__(self):
        super.__init__()
    def raplaceElements(self, dataToBeChanged, incomingData):
        position = self.find(dataToBeChanged)
        position.data = incomingData
    def swapElements(self, swap1, swap2):
        position1 = self.find(swap1)
        prev1 = position1.previous
        next1 = position1.next
        position2 = self.find(swap2)
        prev2 = position2.previous
        next2 = position2.next

        prev1.next = position1
        position1.previous = prev1
        position1.next = next1
        next1.previous = position1

        prev2.next = position2
        position2.previous = prev2
        position2.next = next2
        next2.previous = position2
    def insertBefore(self, incomingData, previousNode=None):
        self.insert(incomingData, self.find(previousNode).previous.data)


class Mylist:
    def __init__(self):
#        self.linkedlist = inheritedLinkedList;
        self.linkedlist = inheritedLinkedList()  # Per martineau's suggestion.

    def replaceElements(self, dataToBeChanged, incomingData):
        self.linkedlist.raplaceElements(dataToBeChanged, incomingData)

    def swapElements(self, swap1, swap2):
        self.linkedlist.swapElements(swap1, swap2)

    def insertFirstM(self, incomingDataM):
        self.linkedlist.insertFirst(incomingDataM)

    def insertLast(self, incomingData):
        self.linkedlist.insert(incomingData)

    def insertAfter(self, incomingData, incomingNode):
        self.linkedlist.insert(incomingData, incomingNode)

    def insertBefore(self, incomingData, incomingNode):
        self.linkedlist.insert(incomingData, incomingNode)

    def remove(self, incomingData):
        self.linkedlist.remove(incomingData)

    def listprint(self):
        self.linkedlist.listprint()


test = Mylist()
test.insertFirstM(4)

导入的LinkedList模块(LinkedList.py)的代码可以根据需要从mygithub repository下载。你知道吗


Tags: pytestselfinitdefnextpreviousmylist
1条回答
网友
1楼 · 发布于 2024-09-30 20:35:04

正如我在评论中所说,您没有正确使用^{}内置函数。试着这样做(就像链接文档中的例子):

class inheritedLinkedList(myLinkedList.DoublyLinkedList):
    def __init__(self):
        super().__init__()  # Change line to this.

实际上,由于派生类__init__()目前除了此之外什么也不做,因此甚至没有必要这样做,因为如果子类没有定义自己的子类,则会自动发生这种情况。换言之,以下内容将完成相同的任务:

class inheritedLinkedList(myLinkedList.DoublyLinkedList):
#    ** NOT REALLY NEEDED AT ALL **
#    def __init__(self):
#        super().__init__()

另外,您还应该更改LinkedList.py脚本的结尾,以便在import作为模块被lists.py编辑时不会执行最后几行:

            ...
            nextNode.previous = previousNode
            dataToBeDeleted.next = dataToBeDeleted.previous = None

if __name__ == '__main__':  # Should add this, too.

    list1 = SinglyLinkedList()
    list2 = DoublyLinkedList()

    list2.insertFirst(6)

相关问题 更多 >