首先,我使用的是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下载。你知道吗
正如我在评论中所说,您没有正确使用^{} 内置函数。试着这样做(就像链接文档中的例子):
实际上,由于派生类
__init__()
目前除了此之外什么也不做,因此甚至没有必要这样做,因为如果子类没有定义自己的子类,则会自动发生这种情况。换言之,以下内容将完成相同的任务:另外,您还应该更改
LinkedList.py
脚本的结尾,以便在import
作为模块被lists.py
编辑时不会执行最后几行:相关问题 更多 >
编程相关推荐