我试图在Queue类中实现需要魔术方法的python Queue类。得到以下错误。
追踪(最近一次通话): 入队1,2,3,4,5
main()
[1] 文件“C:/Users/NGAHU-MWANGI/Desktop/lab10.py”,第55行,主要 数字+ = 15 [2,1] 文件“C:/Users/NGAHU-MWANGI/Desktop/lab10.py”,第22行, iadd [3,2,1] 我在other.container: AttributeError:'int'对象没有属性'container' [4,3,2,1] [5,4,3,2,1]
[5,4,3,2]
项目出列:2 [5,4,3] 项目出列:3 [5,4] 项目出列:4 [5] 项目出列:5 []
class Queue:
def __init__(self,numbers=None):
self.numbers=[]
def is_empty(self):
return self.numbers == []
def enqueue(self, number):
self.numbers.insert(0,number)
def dequeue(self):
return self.numbers.pop()
def __iadd__(self,other):
for i in other.container:
self.add(i)
return self
def __str__(self):
return str(self.numbers)
# -----------------------------------------------------
def main():
numbers = Queue("Numbers")
print("Enqueue 1, 2, 3, 4, 5")
print("---------------------")
for number in range(1, 6):
numbers.enqueue(number)
print(numbers)
print("\nDequeue one item")
print("----------------")
numbers.dequeue()
print(numbers)
print("\nDeque all items")
print("---------------")
while not numbers.is_empty():
print("Item dequeued:", numbers.dequeue())
print(numbers)
# Enqueue 10, 11, 12, 13, 14
for number in range(10, 15):
numbers.enqueue(number)
# Enqueue 15
numbers += 15
#numbers.enqueue(15)
print("\n10, 11, 12, 13, 14, 15 enqueued")
print("-------------------------------")
print(numbers)
# -----------------------------------------------------
main()