【发布时间】:2017-02-21 17:55:07
【问题描述】:
我正在实现一个搜索算法 (BFS) 并拥有以下 Node 类:
class Node:
def __init__(self, state=None, action=None, path_cost=None, parent=None):
self._state = state
self._action = action
self._path_cost = path_cost
self._parent = parent
我的 BFS 求解器返回求解节点(子节点)。例如,使用这个节点,我可以按如下方式计算总路径成本(此代码是另一个 Summary 类的一部分):
def path_cost(self):
self._cost = self._node.path_cost
node = self._node.parent
while node:
self._cost += node.path_cost
node = node.parent
return self._cost
有没有更好的方法通过在Node 中创建自定义__iter__ 方法来做到这一点?
【问题讨论】:
标签: python search tree iterator