英文:
Why am I getting object has no attribute?
问题
queue.py
文件中存在一个拼写错误,导致了该错误。在 Queue
类的构造函数中,def __int__(self):
应该是 def __init__(self):
。请将其更正如下:
class Queue:
def __init__(self): # 修正拼写错误
self._items = deque()
# 其他类方法不需要更改
这样修正后,你的代码应该能够正常运行而不再出现 "AttributeError" 错误。
英文:
I have written a code that uses python deque from collections. I have created an adapetr class queue.py
that handles queue operations. But I am getting error object has no attribute.
queue.py
:
from collections import deque
class Queue:
def __int__(self):
self._items = deque()
def enqueue(self, item):
self._items.append(item)
def extend(self, *elements):
self._items.extend(elements)
def dequeue(self):
try:
return self._items.popleft()
except IndexError:
raise IndexError("Dequeue from empty queue") from Queue
def __len__(self):
return len(self._items)
def __contains__(self, item):
return item in self._items
def __iter__(self):
yield from self._items
def __repr__(self):
return f"Queue({list(self._items)})"
graph.py
:
from queue import Queue
def getNodeName(rootName):
return input(f"Enter child of node {rootName} : ")
def inputChildren(root):
print(f"Enter stop to stop entering children for node {root} : ")
child = list()
while True:
data = getNodeName(root)
if data.lower() == "stop":
break
child.append()
return child
def inputGraph():
graph = dict()
queue = Queue()
queue.enqueue(input("Enter root name : "))
while len(queue) != 0:
root = queue.dequeue()
rootChildren = inputChildren(root)
queue.extend(rootChildren)
if len(rootChildren) == 0:
continue
else:
graph[root] = rootChildren
if __name__=="__main__":
print(inputGraph())
Output :
Enter root name : A
Traceback (most recent call last):
File "D:\graph.py", line 36, in <module>
print(inputGraph())
File "D:\graph.py", line 23, in inputGraph
queue.enqueue(input("Enter root name : "))
File "D:\queue.py", line 9, in enqueue
self._items.append(item)
AttributeError: 'Queue' object has no attribute '_items'
I have tried searching online but cannot figure out why I am getting this error.
答案1
得分: 0
from collections import deque
class Queue:
def __init__(self):
self._items = deque()
def enqueue(self, item):
self._items.append(item)
def extend(self, *elements):
self._items.extend(elements)
def dequeue(self):
try:
return self._items.popleft()
except IndexError:
raise IndexError("Dequeue from empty queue") from Queue
def __len__(self):
return len(self._items)
def __contains__(self, item):
return item in self._items
def __iter__(self):
yield from self._items
def __repr__(self):
return f"Queue({list(self._items)})"
def getNodeName(rootName):
return input(f"Enter child of node {rootName} : ")
def inputChildren(root):
print(f"Enter stop to stop entering children for node {root} : ")
child = list()
while True:
data = getNodeName(root)
if data.lower() == "stop":
break
child.append(data)
return child
def inputGraph():
graph = dict()
queue = Queue()
queue.enqueue(input("Enter root name : "))
while len(queue) != 0:
root = queue.dequeue()
rootChildren = inputChildren(root)
queue.extend(rootChildren)
if len(rootChildren) == 0:
continue
else:
graph[root] = rootChildren
return graph
if __name__=="__main__":
print(inputGraph())
英文:
from collections import deque
class Queue:
def __init__(self):
self._items = deque()
def enqueue(self, item):
self._items.append(item)
def extend(self, *elements):
self._items.extend(elements)
def dequeue(self):
try:
return self._items.popleft()
except IndexError:
raise IndexError("Dequeue from empty queue") from Queue
def __len__(self):
return len(self._items)
def __contains__(self, item):
return item in self._items
def __iter__(self):
yield from self._items
def __repr__(self):
return f"Queue({list(self._items)})"
def getNodeName(rootName):
return input(f"Enter child of node {rootName} : ")
def inputChildren(root):
print(f"Enter stop to stop entering children for node {root} : ")
child = list()
while True:
data = getNodeName(root)
if data.lower() == "stop":
break
child.append(data)
return child
def inputGraph():
graph = dict()
queue = Queue()
queue.enqueue(input("Enter root name : "))
while len(queue) != 0:
root = queue.dequeue()
rootChildren = inputChildren(root)
queue.extend(rootChildren)
if len(rootChildren) == 0:
continue
else:
graph[root] = rootChildren
return graph
if __name__=="__main__":
print(inputGraph())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论