由于AttributeError

如何解决由于AttributeError

当我尝试调用属于Frontier类的print_frontier()时,会发生AttributeError: 'nonetype' object has no attribute 'print_puzzle',其中print_puzzle()是State类的成员函数,并在print_frontier()内部被调用

print_frontier()通过使用self.nodes打印每个拼图来打印print_puzzle()中存储的所有拼图。

值得一提的是,当我在主程序中删除frontier.increase_limit()时,代码成功按预期打印了一个难题,我担心问题应该出在increase_limit()内部,但我不能找出问题所在。

class Frontier:

    ...


    def increase_limit(self) -> None:
        nodes_to_remove = []
        nodes_to_add = []
        current_nodes = self.nodes.copy()
        self.nodes.clear()

        for node in current_nodes:

            if node is None:
                raise Exception('"node" is a NoneObject')

            empty_tile_pos = node.get_empty_tile_pos()

            if empty_tile_pos not in [0,1,2]:
                nodes_to_add.append(
                    State(node.puzzle,node.path).move_tile('up'))
            if empty_tile_pos not in [6,7,8]:
                nodes_to_add.append(
                    State(node.puzzle,node.path).move_tile('down'))
            if empty_tile_pos not in [0,3,6]:
                nodes_to_add.append(
                    State(node.puzzle,node.path).move_tile('left'))
            if empty_tile_pos not in [2,5,node.path).move_tile('right'))

            nodes_to_remove.append(node)

        for node in current_nodes:
            if node not in nodes_to_remove:
                self.nodes.append(node)

        self.nodes.extend(nodes_to_add)
        self.depth += 1

    def print_frontier(self):
        for node in self.nodes:
            node.print_puzzle()  # calling a function of the State class

这里是“状态”类供参考:

class State:

    ...

    def print_puzzle(self):
        print()
        for tile in self.puzzle:
            if self.puzzle.index(tile) in [2,8]:
                print(tile,end='\n')
            else:
                print(tile,end=' ')
        print(self.path,end='\n')

这是我测试班级时的主要程序:

start_state = State([0,2,4,6,8,1])
frontier = Frontier(start_state)
frontier.increase_limit(). # works well without this line
frontier.print_frontier()

解决方法

increase_limit方法中,创建一个State实例,在该实例上调用一个方法,并将该方法的返回值附加到列表中。

nodes_to_add.append(
    State(node.puzzle,node.path).move_tile('up')
)

要执行此操作,move_tile()将需要返回self,这在python中并不常见。
如果您不想为State对象使用临时变量,则可以执行以下操作:

nodes_to_add.append(State(node.puzzle,node.path))
nodes_to_add[-1].move_tile('up')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?