你为什么会收到“对象没有属性”的错误消息?

huangapple go评论72阅读模式
英文:

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())

huangapple
  • 本文由 发表于 2023年2月18日 02:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定