希望快速回答!任何帮助方向将不胜感激。我正在努力准备我的期末考试。
这就是我将如何调用类:
>>> Q = priorityQueue()
>>> Q.insert("text",10)
>>> Q.insert("hello", 18)
>>> Q
text 10, hello 18
所以我有一个像这样的课程:
class priorityQueue():
def __init__(self):
self.items = []
self.priorities = []
def insert(self, x, p):
self.items.append(x)
self.priorities.append(p)
#This is where I dont understand how to get it to return how it should.
def __repr__(self):
new = []
for x in range(len(self.items)):
new.append(str(self.items[x])+ " " +str(self.priorities[x]))
return [str(x) for x in new]
这会给我一个错误,例如:
TypeError: __repr__ returned non-string (type list)
谢谢堆栈!