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



首先,我使用的是python 3.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)

如果需要,可以通过从我的 github 存储库下载导入的LinkedList模块 (LinkedList.py( 的代码来获取。

正如我在评论中所说,您没有正确使用内置super。尝试以这种方式执行操作(因此类似于链接文档中的示例(:

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脚本的末尾,以便在lists.py将其import为模块时不会执行最后几行:

...
nextNode.previous = previousNode
dataToBeDeleted.next = dataToBeDeleted.previous = None
if __name__ == '__main__':  # Should add this, too.
list1 = SinglyLinkedList()
list2 = DoublyLinkedList()
list2.insertFirst(6)

相关内容

  • 没有找到相关文章

最新更新